获取当前url
我正在使用以下代码来获取当前的URL
$current_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
有没有其他的方式来获得相同的,或者可能是更好的方式来获取当前的url?
谢谢。
从参考 :
<?php function curPageURL() { $pageURL = 'http'; if ($_SERVER["HTTPS"] == "on") { $pageURL .= "s"; } $pageURL .= "://"; if ($_SERVER["SERVER_PORT"] != "80") { $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]; } else { $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]; } return $pageURL; } ?>
你必须小心依靠服务器variables,这取决于networking服务器想给你什么…这是一个相当安全的方式来获取URL。
$url = ''; if (isset($_SERVER['HTTPS']) && filter_var($_SERVER['HTTPS'], FILTER_VALIDATE_BOOLEAN)) $url .= 'https'; else $url .= 'http'; $url .= '://'; if (isset($_SERVER['HTTP_HOST'])) $url .= $_SERVER['HTTP_HOST']; elseif (isset($_SERVER['SERVER_NAME'])) $url .= $_SERVER['SERVER_NAME']; else trigger_error ('Could not get URL from $_SERVER vars'); if ($_SERVER['SERVER_PORT'] != '80') $url .= ':'.$_SERVER["SERVER_PORT"]; if (isset($_SERVER['REQUEST_URI'])) $url .= $_SERVER['REQUEST_URI']; elseif (isset($_SERVER['PHP_SELF'])) $url .= $_SERVER['PHP_SELF']; elseif (isset($_SERVER['REDIRECT_URL'])) $url .= $_SERVER['REDIRECT_URL']; else trigger_error ('Could not get URL from $_SERVER vars'); echo $url;