简体   繁体   中英

How can I remove parameters on my URL

I have this URL [HTTP_REFERER] => http://localhost/mysystem/my-system/?page=Page1

and I want to convert it to http://localhost/mysystem/my-system/

I am currently using this:

    $parse = parse_url($_SERVER['HTTP_REFERER']);
    $path = http_build_url($_SERVER['HTTP_REFERER'],
        array(
            "scheme" => $parse['scheme'],
            "host" => $parse['host'],
            "path" => $parse['path'],
            "query" => " "
        ),
        HTTP_URL_STRIP_AUTH | HTTP_URL_JOIN_PATH | HTTP_URL_JOIN_QUERY | HTTP_URL_STRIP_FRAGMENT
    );

giving me this error: Fatal error: Call to undefined function http_build_url(). Is there other way on how to do this without having to set up http_build_url()?

Try this out:

function getURLWithoutParams($url) {
    //Getting the position of ? as it is the start of the parameters
    $position = strpos($url, "?");
    //strpos returns false if the search yielded no results. If it is not
    //false, then we need to extract the needed part, as the url has parameters
    if ($position !== false) {
        return substr($url, 0, strpos($url, "?"));
    }
    return $url;
}

Call it with getURLWithoutParams($_SERVER['HTTP_REFERER'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM