我需要检查我是本地主机还是在网络上。
出于多种原因,我不想使用Alias,所以我想通过脚本获得成功。我曾想通过服务器名来做,但是...某事不起作用。
主要思想:
$hostType = getHostType();
function getHostType()
{
if( $_SERVER['SERVER_NAME'] === 'localhost' ) { return 'local'; }
elseif( isset($_SERVER["HTTPS"]) ) { return 'https'; }
else { return 'http'; }
}
if( $hostType = 'local')
{
$serverhost = ('http://'.$_SERVER['HTTP_HOST'].'/myfolder');
$resources = str_replace('//','/', $_SERVER['DOCUMENT_ROOT'].'/myfolder/resources');
}
elseif($hostType ='https')
{
$serverhost ='https://'.$_SERVER['HTTP_HOST'];
$resources = $_SERVER['DOCUMENT_ROOT'].'/resources';
}
else
{
$serverhost = 'http://'.$_SERVER['HTTP_HOST'];
$resources = $_SERVER['DOCUMENT_ROOT'].'/resources';
}
echo "hostype: ".$_SERVER['SERVER_NAME']." -- ".$hostType." |||| server on: ".$serverhost." ||||| resources on: ".$resources;
脚本告诉我,即使看到服务器名称的差异,我也位于本地主机上。
在线和本地,打印此:
hostype:mysite.com-本地服务器在:h t t p // mysite.com / myfolder“资源位于:/ home / asfasr / public_html / mysite / resources
解决方案>>
经过几次测试,我弄清楚了如何从网络上拆分localhost。脚本:
<? $protocol = ''.$_SERVER['REQUEST_SCHEME']; $host = ''.$_SERVER['HTTP_HOST']; $servername = ''.$_SERVER['SERVER_NAME']; $pageurl = ''. $_SERVER['REQUEST_URI']; $project = explode('/', $pageurl)[1]; $pagename = basename($_SERVER['PHP_SELF']); // get host type if( $host == 'local' || $servername == 'localhost' ) { $GetHost = ('http://localhost/'.$project.'/'); $GetFile = $_SERVER['DOCUMENT_ROOT'].$project.'/'; // $GetFile = str_replace('//','/', $_SERVER['DOCUMENT_ROOT'].'/Seexor/'); } elseif( $protocol == 'https') { $GetHost ='https://'.$_SERVER['HTTP_HOST']; $GetFile = $_SERVER['DOCUMENT_ROOT']; } else { $GetHost = 'http://'.$_SERVER['HTTP_HOST'].'/'; $GetFile = $_SERVER['DOCUMENT_ROOT'].'/'; } ?>
现在您还可以通过“ $ GetHost”和“ $ GetFile”来获取文件或媒体
示例:$myPathFiles = $GetFile.'resources/backend/filestoinclude/';
$myPathImage = $GetHost.'resources/imgs/';
感谢那些帮了我的忙。
我需要检查我何时是本地主机还是在网络上。由于多种原因,我不想使用Alias,因此我想通过脚本获得成功。我想通过服务器名来完成,但是...某事...
您不是在检查$ hostType,而是在分配它。使用“ ===”代替“ =”