Thursday, May 10, 2012

PHP - get full URL

There are times when we want to get the full path (URL) of a page, PHP makes it easy

function getPageURL() {
    $pageURL = 'http';

        // Check if https is enabled
    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;
}

Call getPageURL() whenever you want to get the expected path.

No comments:

Post a Comment