我有一个本地和远程服务器,我正在开发一个项目
//local
$servername = "localhost";
$username = "bob";
$password = "fred";
$dbname = "jane";
//remote
$servername = "arthur";
$username = "Milly";
$password = "Horace";
$dbname = "Erastus";
目前我正在评论//本地或//远程位,具体取决于我连接的方式。当然,我经常错误地覆盖连接文件,这很痛苦。
是否有某种方法可以检测到我所在的服务器,然后自动连接。我试过谷歌搜索,但无法改变任何可能因为我想不出正确的搜索条件。
由于
答案 0 :(得分:1)
执行此操作的方法不止一种,但您可以尝试的方法是从IP地址检测您所在的服务器
if ($_SERVER['SERVER_ADDR'] === '127.0.0.1') {
$servername = "localhost";
$username = "bob";
$password = "fred";
$dbname = "jane";
} else {
//remote
$servername = "arthur";
$username = "Milly";
$password = "Horace";
$dbname = "Erastus";
}
答案 1 :(得分:1)
您可以尝试以下代码: -
<?php
if ($_SERVER['SERVER_NAME'] == "localhost") {
$servername = "localhost";
$username = "bob";
$password = "fred";
$dbname = "jane";
}
elseif ($_SERVER['SERVER_NAME'] == "google.com") {
$servername = "arthur";
$username = "Milly";
$password = "Horace";
$dbname = "Erastus";
}
else {
echo "No configuration found!";
exit;
}
答案 2 :(得分:0)
试试这段代码: -
$conn = new mysqli($servername1, $username1, $password1);
if (! $conn->connect_error) {
$conn = new mysqli($servername2, $username2, $password2);
}
或检查: -
$_SERVER['HTTP_HOST']=="localhost"