How to remove the querystring and get only the url?

Im using PHP to build the URL of the current page. Sometimes, URLs in the form of

www.mydomian.com/myurl.html?unwantedthngs

are requested. I want to remove the ? and everything that follows it (querystring), such that the resulting URL becomes:

www.mydomain.com/myurl.html

My current code is this:

<?php
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;
}
?>

15 Answers
15

Leave a Comment