0

I have a strange issue. It almost seems like a bug.

if ($object->error) {Form::BuildReturn(); header ("location:$url");}

Now if I echo the value of $url, I get the correct value: /projects/view/112/?edit-note=105

But when the script executes, it takes me to /projects/view/112/.

Here's the weird part though: I can try to echo or print something right after the header and then it will take me to /projects/view/112/?edit-note=105 like I want it to. Example:

if ($object->error) {Form::BuildReturn(); header ("location:$url"); echo 0;}

I've tried searching this for about half an hour now using terms like 'PHP header query string', 'PHP header get values', and so on. I hope there is some actual solution to this rather than using a hacky workaround that generates 'cannot modify header information' warnings...

3
  • Seems like header("Location:...") doesn't accept Get variables. Does it work if you directly write "/projects/view..." in PHP instead of $url? Maybe you could try adding exit; after the redirection if it works with echo. Commented Dec 30, 2011 at 9:36
  • Doh... didn't even think of exit;... that worked. Thank you. If you post that as an answer I will accept it. Commented Dec 30, 2011 at 9:38
  • 1
    Try an absolute URL, including protocol and server name http://example.com/.. -- Also show a wget -S or Firebug header dump. Also likely you have multiple header() calls in your code, one overwriting the more specific. Commented Dec 30, 2011 at 9:39

5 Answers 5

2

Try adding exit; after redirecting. That stops executing the current file.

Sign up to request clarification or add additional context in comments.

Comments

2

Add

exit(0);

after the header for the redirect.

Because the code continues to run even after the header call. You need to explicitly exit the script for the redirect. This is discussed more here.

Comments

1

The correct syntax is:

header("Location: $url");

There's a space in there and a capital L, I'm not sure that'll make much difference.

For the moment I'd suggest doing some low level debugging. Use wget, curl, the developer mode in your browser or a plugin that will echo all of the HTTP headers and then post up exactly what's being returned in the server headers (if the results don't allow you to figure it out anyway).

Comments

1

You should always exit after you pass a Location header, to prevent the rest of the page from showing. Also, it should fix your problem.


Also, the correct form of the Location header would be:

header("Location: $url"); //Has space and capital L

I don't think it would matter really, only for older browsers maybe?

Comments

0

You could use a redirection function with meta tag. That will keep query string

function redir($url, $time = 0) {
  echo '<meta http-equiv="refresh" content="', $time, ';URL=', $url, '">';
  die;
}

1 Comment

but that always takes some time and is client-sided.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.