PHP确定当前的url

时间:2009-12-08 14:23:16

标签: php url directory

我需要修改我的函数以返回我当前的文件夹。这是我当前的函数:

function getLinkFromHost($url){  
    $port = $_SERVER['REMOTE_PORT'];  
    $server = $_SERVER['HTTP_HOST'];  
    if($port == 443){  
        $type = "https";  
    } else {  
        $type = "http";  
    }  
    return $type . "://" . $server . "/" . $url;  
}

5 个答案:

答案 0 :(得分:8)

查看$_SERVER['REQUEST_URI']$_SERVER['SCRIPT_NAME']

(来自$_SERVER手册)

答案 1 :(得分:5)

这是一个很短暂的甜蜜功能,​​我已经用了一段时间了。

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;
}

我不能信用,它属于:

http://www.webcheatsheet.com/PHP/get_current_page_url.php

答案 2 :(得分:1)

..........

echo $_SERVER['PHP_SELF']; // return current file

echo __FILE__; // return current file

echo $_SERVER['SCRIPT_NAME']; // return current file

echo dirname(__FILE__); // return current script's folder

// etc

答案 3 :(得分:1)

也许您还希望将vars包含在您的网址中,因此您应该向 highphilosopher 功能添加内容......

$current_url = rtrim(curPageURL(), "/").(!empty($_GET)) ? "?".http_build_query($_GET) : "";

答案 4 :(得分:1)

这是一种可能有用的方法:

function current_url()
{
    $result = "http";

    if($_SERVER["HTTPS"] == "on") $result .= "s";

    $result .= "://".$_SERVER["SERVER_NAME"];

    if($_SERVER["SERVER_PORT"] != "80") $result .= ":".$_SERVER["SERVER_PORT"];

    $result .= $_SERVER["REQUEST_URI"];

    return $result;
}