views:

87

answers:

3

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?

Thanks

+1  A: 
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
A: 

There isn't a native way as far as I know, nute 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;
}
harpax
+1  A: 

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