4

I'm using a function called headerLocation() in order to redirect properly. This is the pertinent part of the function, I'm using it to redirect and then display an appropriate message (personal preference).

function headerLocation($location,$message)
{
    $_SESSION['output'] = $message;
    header("Location: ". $location);
}

The problem is, when I run the script, the header("Location: ". $location); part is activated without even calling the function (when I initially require it for php parsing). This renders the function useless.

Is there any way around this?

Gal
  • 23,122
  • 32
  • 97
  • 118
  • 4
    There *must* be some call of `headerLocation` or another of `header` somewhere. Functions are not called upon declaration. – Gumbo Jan 22 '10 at 00:23

5 Answers5

10

In addition to the feedback already given. It is a good idea to exit after you redirect.

function headerLocation($location,$message)
{
    $_SESSION['output'] = $message;
    header("Location: ". $location);
    exit;
}
danielrsmith
  • 4,050
  • 3
  • 26
  • 32
  • It took me 1 hour to realize why some function keep calling after a "header('Location:index.php');" until i read this comment. After i added exit, no more errors. Thanks – Alex Sep 26 '14 at 16:34
3

That should not happen. Either you are calling the function somewhere, or another part of the code is writing out the header. Can you add some debug to that function to check?

Bobby Jack
  • 15,689
  • 15
  • 65
  • 97
1

The redirect happens too fast for the $_Session to be written properly. Use

session_write_close();

before the header call.

Edit: Removed the ridiculous $ in front of the function call.

Joel Etherton
  • 37,325
  • 10
  • 89
  • 104
  • I don't think that is what OP is asking. I believe OP wants to know why the `header()` call is excuted without calling the `headerLocation` function. – Decent Dabbler Jan 22 '10 at 00:23
  • No, he mentions that the header() is called so the headerLocation() is called, the previous lines just don't get executed because the thread is destroyed before the $_SESSION can be updated. If his function didn't get called, the redirect would never occur. Forcing the session to write before it can move on to the next directive causes the thread to pause long enough to finish before the headers are pushed out and redirected. – Joel Etherton Jan 22 '10 at 00:26
  • although I definately agree with your proposed safety precaution, I think you might want to read the question again. ;-) – Decent Dabbler Jan 22 '10 at 00:36
  • 1
    ... and rewrite your code so that it doesn't call a function whose name is the contents of a variable named 'session_write_close' ;-) – Bobby Jack Jan 22 '10 at 00:41
1

Just a guess, perhaps you should buffer your output ( ob_start() ) This will cause headers to be sent only when the output is flushed, allowing the rest of your code to execute.

davidosomething
  • 3,379
  • 1
  • 26
  • 33
1

It makes no sense for the header() function to redirect without calling headerLocation() first.

My guess is that you're not seeing $_SESSION['output'] hold $message, and that makes you think the function is not being executed. Try writing to a new file instead, does that work? I bet it will.

The reason $_SESSION might not be holding your values is probably because of P3P and your browser and / or PHP configuration.

Also, are you sure you don't wanna call die() / exit() after the header() redirect?

Community
  • 1
  • 1
Alix Axel
  • 151,645
  • 95
  • 393
  • 500
  • as I said, this is only a part of the function. and i'm not confused, it's not because of $message. – Gal Jan 22 '10 at 00:30
  • @Gal: What makes you think `headerLocation()` is not being executed then? – Alix Axel Jan 22 '10 at 00:31
  • It is being executed, but not when I'm calling it. I gets executed when the function is parsed by php because before I use it, I echo something and exit; and still it gets redirected. – Gal Jan 22 '10 at 00:35
  • 1
    Ok, i disabled one of my calls to a mysql function, it works well. That's the problem. I'll try to fix it. – Gal Jan 22 '10 at 00:37
  • 1
    @Gal: That's impossible, check your code again, double check to make sure you got the right number of open and close brackets, etc... – Alix Axel Jan 22 '10 at 00:37
  • @Gal: That's just not how PHP works (caveat: unless you've discovered an insanely obscure bug, which is very unlikely) so you must either be calling the function from some other code, or the function isn't executing at all, but some other code IS and it LOOKS like this is getting called. – Bobby Jack Jan 22 '10 at 00:39
  • 1
    Alix Axel thanks, you were indeed right. there was a hidden redirection inside the mysql function. – Gal Jan 22 '10 at 00:40
  • There is a known bug in Gecko browsers where any non-header information sent *after* a header-redirect will cause the browser to NOT honor the redirect so you SHOULD use die/exit explicitly. – John Jul 27 '14 at 11:11