【问题标题】:WordPress wp-config.php if-else is not working on some computersWordPress wp-config.php if-else 在某些计算机上不起作用
【发布时间】:2014-02-20 09:45:02
【问题描述】:

我在本地和远程设置中有相同的 wp-config。

下面的代码检查用户是在加载站点的本地版本还是实时版本,并使用适当的数据库凭据:

/* 
 * Unified variables 
 */   
$hostname = 'localhost';  
$chartset = 'utf8';  
*etcetc....

/* 
 * Check for the current environment 
 */  
if ($_SERVER["HTTP_HOST"] === 'example.dev') {  
  $db_name = 'example';  
  $user_name = 'root'; 
  $password = 'root';  
} else 
{  
  $db_name = 'remote_db_name';
  $user_name = 'remote_db_user';
  $password = 'remote_db_pw; 
}  
 
// ** MySQL settings - You can get this info from your web host ** //  
/** The name of the database for WordPress */  
define('DB_NAME', $db_name);  
  
/** MySQL database username */  
define('DB_USER', $user_name);  
//etc....

这个有效。但是,如果我将 if 命令翻转过来,那么它的

if ($_SERVER["HTTP_HOST"] === 'example.com') {  
  $db_name = 'example';  
  $user_name = 'root'; 
  $password = 'root';  
} else 
{  
  $db_name = 'remote_db_name';
  $user_name = 'remote_db_user';
  $password = 'remote_db_pw; 
}  

它适用于几种不同的设备和连接,但对于我的客户端它不再适用。任何想法为什么会这样?

我需要相反,因为我正在使用其他设备测试我的设计,这些设备的网址如下: http://example.dev.192.168.1.3.xip.io 如果本地开发的所有内容(例如 example.dev 和 http://example.dev.192.168.1.3.xip.io)都是 if-else 中的“else”-case,那就更好了。

什么可能导致我的客户端出现数据库错误?他已经尝试了几种设备和连接,如果实时服务器处于“其他”情况而不是

if ($_SERVER["HTTP_HOST"] === 'example.com') {  

【问题讨论】:

    标签: php mysql database wordpress if-statement


    【解决方案1】:

    或许可以考虑:

    if ( stristr( $_SERVER['HTTP_HOST'], 'example.dev' ) ) {
    
    }
    

    这适用于您上面给出的示例。您甚至可以尝试将 example.com 作为指针。我很好奇是否 www.将显示在 HTTP_HOST 中。如果是这样,这可能是您的客户遇到问题的原因吗?

    【讨论】:

    • 不错的一个!事实上,我在我的网站上回应了 $_SERVER["HTTP_HOST" 并且我的客户正在浏览网站的 www 版本。因为我在 support.clickontyler.com/a/21/wordpress-liftoff 上使用了最后一个解决方案,它使自动 wordpress 重定向停止工作。
    猜你喜欢
    • 1970-01-01
    • 2014-07-02
    • 2011-12-22
    • 1970-01-01
    • 2014-07-19
    • 2014-04-28
    • 2020-12-22
    • 1970-01-01
    • 2020-12-29
    相关资源
    最近更新 更多