简体   繁体   English

WordPress wp-config.php if-else 在某些计算机上不起作用

[英]WordPress wp-config.php if-else is not working on some computers

I have the same wp-config in local and remote setups.我在本地和远程设置中有相同的 wp-config。

The code below checks if user is loading the local or the live -version of the site, and uses appropriate database credentials:下面的代码检查用户是否正在加载站点的本地或实时版本,并使用适当的数据库凭据:

/* 
 * 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....

This one works.这个有效。 But if I flip the if command other way around so its但是如果我以其他方式翻转 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; 
}  

it works for me with several different devices and connections, but for my client it no longer works.它适用于我使用几种不同的设备和连接,但对于我的客户它不再适用。 Any ideas why this could be?任何想法为什么会这样?

I need it to be the other way around because I'm testing my designs with other devices with urls like: http://example.dev.192.168.1.3.xip.io and it would be better if all the instenses of local development like example.dev and http://example.dev.192.168.1.3.xip.io would be the "else" -case in the if-else.我需要它是另一种方式,因为我正在使用其他设备测试我的设计,网址如下: http : //example.dev.192.168.1.3.xip.io ,如果所有本地开发的强度都会更好像 example.dev 和http://example.dev.192.168.1.3.xip.io将是 if-else 中的“else”-case。

What could lead to database error on my client?什么可能导致我的客户端出现数据库错误? He has tried several devices and connections and the problem goes away if the live-server is in the "else" case and not the他尝试了几种设备和连接,如果实时服务器处于“其他”情况而不是“其他”情况,问题就会消失。

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

Maybe consider something like:也许考虑这样的事情:

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

}

This will work for the examples you gave above.这将适用于您上面给出的示例。 You could even try example.com as the needle.您甚至可以尝试 example.com 作为指针。 I'm curious as to whether www.我很好奇 www. would show up in the HTTP_HOST.会出现在 HTTP_HOST 中。 If so could this be why your client is having an issue?如果是这样,这可能是您的客户遇到问题的原因吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM