PHP如果是localhost,则 [英] PHP If localhost then
问题描述
你好,
我想知道是否有一些PHP代码可以判断它是从在线Web服务器运行还是从本地主机服务器运行.
我之所以这样问,是因为注释掉了将在本地主机而不是在线Web服务器上运行的代码.
例如,对于MYSQL,我的登录凭据与在线MYSQL凭据完全不同.
我正在从免费的在线Web服务器使用Filezilla传输FTP,Xampp传输本地主机,以及www.000webhost.com.只需将有效的localhost php文件通过FTP传输到Web服务器,而无需修改代码.
这就是我的想法.
我使用一些If,ElseIF语句来实现这一目标
Hello,
I was wondering if it is possible for some PHP code to tell if it is running from a online web server or from a local host server.
I ask this because commenting out code that will run on a localhost but not an online web server.
For example with MYSQL, my login credentials are completely different from the online MYSQL credentials.
I am using Filezilla for my FTP, Xampp for my localhost and www.000webhost.com from a free online web server. It would be nice just to FTP a working localhost php file to the web server without modifying the code.
Here is what I am thinking.
I use some If, ElseIF statements to achieve this
<?php
if ($localhost == TRUE) {
mysql_connect("localhost", "test", "") or die(mysql_error());
echo "Connected to Localhost<br />";
}
elseif (($localhost == FALSE) {
mysql_connect("mysql3.000webhost.com", "username", "password") or die(mysql_error());
echo "Connected to online server<br />";
}
?>
I don't know if this is the right way of doing this. Can someone please help? thanks.
推荐答案
localhost == TRUE){ mysql_connect(" ," 测试", ")或 die(mysql_error()); echo " ; } elseif(((
localhost == TRUE) { mysql_connect("localhost", "test", "") or die(mysql_error()); echo "Connected to Localhost<br />"; } elseif ((
localhost == FALSE){ mysql_connect(" ," 用户名", 密码")或 die(mysql_error()); echo " ; } ?> 我不知道这是否是正确的方法.有人可以帮忙吗?谢谢.
localhost == FALSE) { mysql_connect("mysql3.000webhost.com", "username", "password") or die(mysql_error()); echo "Connected to online server<br />"; } ?> I don't know if this is the right way of doing this. Can someone please help? thanks.
几种方式:
1.(可能不可靠)查看请求URL是否包含字符串"localhost"
2.在XAMPP配置中,设置唯一的服务器名称,然后测试
Several ways:
1. (may be unreliable) see if the request URL includes the string ''localhost''
2. In your XAMPP config, set a unique server name, then test
这篇关于PHP如果是localhost,则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!