Search code examples
phpurlheadertexttrimming

How to trim $_SERVER['HTTP_REFERER']


After from processing i am sending the user on the previous page using:

header('Location: ' . $_SERVER['HTTP_REFERER'] . '?Add=Successful');

Now it sends me to my add.php:

http://localhost/add.php?Add=Successful

Again when i add one more data the header location passes the following:

http://localhost/add.php?Add=Successful?Add=Successful

What i want is to trim the header location till question mark:

Lets say something like trimming the $_SERVER['HTTP_REFERER'] till ? and saving it into a variable so that if keyword ? exists it should trim it again to http://localhost/add.php and then pass that variable into header location, so that it can become something like this:

header('Location: ' . $trimmedHeader . '?Add=Successful');

Solution

  • You can also use PHP parse_url() function.

    $url = parse_url($_SERVER['HTTP_REFERER']);
    $trimmedHeader = $url['scheme'] . '://' . $url['host'] . $url['path'];
    header('Location: ' . $trimmedHeader . '?Add=Successful');