How to get current page URL in PHP

2018 / 05 / 09

Do this

    echo $url;

More efficient code:

function getCurrentPageUrl() {
    $pageURL = 'http';
    if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
    $pageURL .= "://";
    if ($_SERVER["SERVER_PORT"] != "80") {
    } else {
    return $pageURL;

echo getCurrentPageUrl();


You can write the above code in functions file of your of your web software.

You can also fetch URL in WordPress but in this post we have mentioned native PHP solution.

Hi, I am a full-stack web developer with 5+ years of experience in working with different web technologies. Do you want to ask something? just send me a message through the contact form. Please visit my portfolio at Thanks