3

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');
Naki
  • 1,616
  • 1
  • 15
  • 17
Django Anonymous
  • 2,987
  • 16
  • 58
  • 106

4 Answers4

9

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');
Naki
  • 1,616
  • 1
  • 15
  • 17
  • All of you have given the answer and its working, but the problem is, its not passing the variable back along with header. Its passing this:- `http://localhost/add.php` whereas i want to pass this:- `http://localhost/add.php?Add=Successful` again. – Django Anonymous Mar 29 '12 at 12:43
  • But this what is in my script pass this variable again: '?Add=Successful'. What more do You need? – Naki Mar 29 '12 at 12:55
0
preg_replace('/(.*)\?/',$_SERVER['HTTP_REFERER'],'\1');
kappa
  • 1,559
  • 8
  • 19
0

This will return you everything before the first question mark in the string.

$trimmedheader = array_shift(explode("?", $_SERVER['HTTP_REFERER']));
Pierre-Olivier
  • 3,104
  • 19
  • 37
  • All of you have given the answer and its working, but the problem is, its not passing the variable back along with header. Its passing this:- `http://localhost/add.php` whereas i want to pass this:- `http://localhost/add.php?Add=Successful` again. – Django Anonymous Mar 29 '12 at 12:43
  • You said: `What i want is to trim the header location till question mark` That is what my script is doing. If you want to append "?Add=Successful" to your string, just add it at the end: `array_shift(explode("?", $_SERVER['HTTP_REFERER'])) . "?Add=Successful"` – Pierre-Olivier Mar 29 '12 at 12:49
0
$urlArray = parse_url($_SERVER['HTTP_REFERER']);
$newUrl = $urlArray['scheme'].'://'.$urlArray['host'].$urlArray['path'].'?Add=Successful';
header("Location: $newUrl");

This has been tested and works fine....

Ingmar Boddington
  • 3,440
  • 19
  • 38
  • All of you have given the answer and its working, but the problem is, its not passing the variable back along with header. Its passing this:- `http://localhost/add.php` whereas i want to pass this:- `http://localhost/add.php?Add=Successful` again. – Django Anonymous Mar 29 '12 at 12:42
  • Try concatenating outside the header line maybe (I know function returns in header strings foofs things up) – Ingmar Boddington Mar 29 '12 at 13:41