9

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?

Skatox
  • 4,237
  • 12
  • 42
  • 47
eozzy
  • 66,048
  • 104
  • 272
  • 428

5 Answers5

13
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)); }
Sorantis
  • 14,496
  • 5
  • 31
  • 37
9

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;
}
danielson317
  • 3,121
  • 3
  • 28
  • 43
harpax
  • 5,986
  • 5
  • 35
  • 49
  • It could be good if you also check `$_SERVER["SERVER_PROTOCOL"]` to know the protocol. – PhoneixS May 14 '14 at 07:13
  • @PhoneixS Suppose it's "HTTP/1.1" ... what do I do with that? – Michael Dec 10 '14 at 03:05
  • @Michael you know that the protocol is http so you can add http to the url. In this answer you assumed that all request are http but it's possible to have others protocols to serve the files (for example in an apache server with mod_ftp I think it can be ftp). – PhoneixS Dec 11 '14 at 09:20
  • @PhoneixS Ah ok, it seems to not be something I could use directly, i.e. I have to map HTTP/1.1 (or HTTP/1.0 or whatever else) to http://, except it might be https:// instead, and so I don't really know what other possible values might map to what. It doesn't seem to be a simple 1:1 mapping anyway, some interpretation is required. – Michael Dec 11 '14 at 15:10
  • @Michael you can do like @Sorantis answer `strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/")` to extract the protocol. It's very strange that you will see a server configured to serve php pages trough other protocol than http so normally it's save to omit this check. I commented it only in case you were one of this strange servers. – PhoneixS Dec 12 '14 at 09:28
  • What I am missing here is, the port for ssl. When the connection is ssl, the port should be added if not 443 rather then 80 – André van Schoubroeck Mar 27 '15 at 12:34
3

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.

2

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;
  }
Teja Kantamneni
  • 17,402
  • 12
  • 56
  • 86
2

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;
Muhammad Ashikuzzaman
  • 3,075
  • 6
  • 29
  • 53