Getting the full URL of the current page (PHP) [du

2019-01-17 12:58发布

This question already has an answer here:

I'm working on this page: http://localhost/projectname/custom.php

Both <?php echo $_SERVER['REQUEST_URI']; ?> and <?php echo $PHP_SELF; ?> don't give full location. What should I use to grab the full url location?

5条回答
地球回转人心会变
2楼-- · 2019-01-17 13:31

If you are trying to add variables back onto the end of an URL that you are passing through a link tracking script, for example, you could try this:

$URI = array();
foreach($_GET as $key=>$val)
{
   if ($key!="link"&&$key!="id"&&$key!="type") $URI[] = "$key=".urlencode($val);
}
if (sizeof($URI)>0) $link.="&".join("&",$URI);

In this case, "link", "id" and "type" were the variables I needed for the tracking, but the URL I wanted to track had a variable on the end of it that got stripped off by my script as if it was part of the query being sent to it; I needed the add it back to the link URL before passing it to header("Location:".$link).

If this is what you are trying to achieve this works great and is shorter than above example.

查看更多
甜甜的少女心
3楼-- · 2019-01-17 13:32

There isn't a native method as far as I know, but you could use this:

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;
}
查看更多
时光不老,我们不散
4楼-- · 2019-01-17 13:35

check this one... a bit long and dirty but works good...

 function absolutizeUrl ( $u, $p )
 {
    $url = parse_url( $u );
    $page = parse_url( $p );

    if ( strpos( $u , '/' ) === 0 )
    {
            //already absolute              
    } else {
            $basePath = '';
            if (
                    isset( $page[ 'path' ] )
                    && strpos( ltrim( $page[ 'path' ], '/' ), '/' )
            )
            {
                    $baseTokens = explode( '/', $page[ 'path' ] );
                    array_pop( $baseTokens ); // strip basename                     
                    $baseTokens[] = $u;
                    $u = join( '/', $baseTokens );
            }
    }
    if ( ! isset( $url[ 'host' ]))
    {
            $u = 'http://'.$page[ 'host' ].'/'.ltrim( $u, '/' );
    }
    return $u;
  }
查看更多
我欲成王,谁敢阻挡
5楼-- · 2019-01-17 13:53

I found this code very helpful

$protocol = strpos(strtolower($_SERVER['SERVER_PROTOCOL']),'https') === 
FALSE ? 'http' : 'https';            // Get protocol HTTP/HTTPS
$host     = $_SERVER['HTTP_HOST'];   // Get  www.domain.com
$script   = $_SERVER['SCRIPT_NAME']; // Get folder/file.php
$params   = $_SERVER['QUERY_STRING'];// Get Parameters occupation=odesk&name=ashik

$currentUrl = $protocol . '://' . $host . $script . '?' . $params; // Adding all

echo $currentUrl;
查看更多
等我变得足够好
6楼-- · 2019-01-17 13:54
function selfURL() 
{ 
    $s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : ""; 
    $protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s; 
    $port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]); 
    return $protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI']; 
} 

function strleft($s1, $s2) { return substr($s1, 0, strpos($s1, $s2)); }
查看更多
登录 后发表回答