通过脚本确定您是在本地主机上还是在网络上
Find out if you are on localhost or web via script
我需要检查我是本地主机还是在网络上。
我不想用Alias因为很多原因所以我想用一个脚本来成功。我想通过 servername 来做,但是...有些东西不工作。
主要思想:
$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 -- local
server on: h t t p//mysite.com/myfolder"
resources on: /home/asfasr/public_html/mysite/resources
解决方案
经过几次测试,我想出了如何从网络中分离本地主机。
剧本:
<?
$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/';
感谢那些帮助过我的人。
您不是在检查 $hostType,而是在分配它。
使用“===”代替“=”
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';
}
我需要检查我是本地主机还是在网络上。
我不想用Alias因为很多原因所以我想用一个脚本来成功。我想通过 servername 来做,但是...有些东西不工作。
主要思想:
$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 -- local server on: h t t p//mysite.com/myfolder" resources on: /home/asfasr/public_html/mysite/resources
解决方案
经过几次测试,我想出了如何从网络中分离本地主机。 剧本:
<?
$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/';
感谢那些帮助过我的人。
您不是在检查 $hostType,而是在分配它。 使用“===”代替“=”
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';
}