views:

2351

answers:

5

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?

+1  A: 

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
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.
fireeyedboy
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
@Joel: although I definately agree with your proposed safety precaution, I think you might want to read the question again. ;-)
fireeyedboy
... 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
Oh, yah. Woops. Syntax error.
Joel Etherton
+2  A: 

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
+1  A: 

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
+1  A: 

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?

Alix Axel
as I said, this is only a part of the function.and i'm not confused, it's not because of $message.
sombe
@Gal: What makes you think `headerLocation()` is not being executed then?
Alix Axel
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.
sombe
Ok, i disabled one of my calls to a mysql function, it works well. That's the problem. I'll try to fix it.
sombe
@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
@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
Alix Axel thanks, you were indeed right. there was a hidden redirection inside the mysql function.
sombe
+2  A: 

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