$SERVER变量和双localhost


$SERVER variable and the double localhost

我已经构建了一个函数,它基本上将摘录链接到它的相关帖子。为了做到这一点,我认为变量$_SERVER['SERVER_NAME']可以在构建路径时派上用场。

我不知道这是否是由于我在本地环境中测试页面,但在我的本地机器上,我拥有的路径就像http://localhost/webdir/localhost/index.php?p=3 localhost重复两次。是什么原因导致的呢?

试试这个编辑工作伟大的几乎所有类型的URL..

<?php
$l1=explode('?',$_SERVER["SERVER_NAME"]);
$link='http://'.$l1[0];
echo $link;
echo '<h2><center>Add users</center></h2>';
$pageURL = $_SERVER['HTTPS'] == 'on' ? 'https://' : 'http://';
$pageURL1 = $_SERVER['SERVER_PORT'] != '80' ? $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"] : $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$pageURL=$pageURL.$pageURL1;
//$pageURLt=explode('?',$pageURL);//Uncomment this and next line if you don't want get variable
//$pageURL=$pageURL.$pageURLt[0];
echo $pageURL;
?>

像这样使用

$link='http://'.$SERVER_['SERVER_NAME'];

如果你不想要任何GET类型的值使用这个

$l1=explode('?',$SERVER_['SERVER_NAME']);
$link='http://'.$l1[0];