Wednesday, 18 September 2013

PHP: how to get current page url (Windows/IIS + Linux)

In many times we need to get current page url using PHP. But there are some different system parameters between Linux and Windows so that some function can be good in linux server but cannot run on Windows example Xampp on windows. The following solutions can solve the problem.

function getURL()
    {
        $pageURL = 'http';
        if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') { // SSL connection
            $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;
    }

You also can custom it to get other system parameters base on $_SEVER variable.
learn more : PHP $_SERVER manual.

0 nhận xét:

Post a Comment