Quantcast
Channel: php – Windows – Linux – Mac OSX Guides
Viewing all articles
Browse latest Browse all 11

Get the complete url of the current web page with PHP

$
0
0

Going to share with you a small snippet today, to determine and get the complete url of the current page in PHP

1. The Function

function getPageUrl()
{
 
    // First we see if the request was over SSL (HTTPS)
    if( isset( $_SERVER['HTTPS'] ) && !empty( $_SERVER['HTTPS'] ) && ( strtolower( $_SERVER['HTTPS'] ) != 'off' ) )
    	$protocol = 'https';
    else
    	$protocol = 'http';
    $url = $protocol.'://';
    // Now check for the port
    if ($_SERVER["SERVER_PORT"] != "80") {
        $url .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
    } else {
        $url .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    }
    return $url;
}

2. Usage

//...
$url = getPageUrl();
echo $url;
//...

Share


Viewing all articles
Browse latest Browse all 11

Trending Articles