PHP redirects, sessions, and etc in Firefox

He has: 14 posts

Joined: Sep 2005

Hi,

I am trying to figure out what's going on. Here is what I encounter.
First off, a few notes:
LOG IN
1.) the root domain redirects immediately to a subdirectory (e.g. http://www.example.com redirects immediately to http://www.example.com/mydir)
2.) the above is done with
<meta HTTP-EQUIV='refresh' content=0;url='/mydir'>

LOG OUT
1.) the logout page is supposed to redirect back to the main page, using:

<?php
echo \"<meta HTTP-EQUIV='refresh' content=0;url='/mydir'>\";
?>

2.) the above is done after session has been destroyed, using:
<?php
session_destroy
();
?>

The QUESTION:
This redirection behavior works perfectly in IE, never failed once (as far as I can remember). For some odd reason, Firefox doesn't seem to like it. It sometimes "freezes up" (the redirect does not work, and the user just sits at an empty page). Refresh at this point does NOT solve the problem. The only option was to close and reopen the browser.

I can't find out what is causing this erratic behavior. Can anyone please tell me what I'm doing incorrectly?

Thanks so much!

dk01's picture

He has: 516 posts

Joined: Mar 2002

One question I have is why are you using the meta refresh? Are you required to? If not I would strongly suggest you use PHP's header() function instead. It works on the server before a page is served and therefore allows for you to forget about what browser the user is using. Its proper use is:

<?php
<php header(\"Location: URLHERE\");
?>

You can only use this before any content has been sent to the browser otherwise you will get an error but it is a solid solution to your problem. Hope this helps some.

-Jim

He has: 14 posts

Joined: Sep 2005

Hi dk01,

Thanks for your suggestion! Unfortunately, I think this only solves the login problem (please correct me if I'm mistaken). In my logout page, I do some database cleanup code and that must occur before the redirect. So I guess that the

<?php
header
(\"Location: ...\");
?>
.. function won't help? Is there anything else I can do?

A related question: Is meta refresh the only way to do redirects if there must be code before the redirect? Does anyone know of a better solution?

Thanks again!

dk01 wrote: One question I have is why are you using the meta refresh? Are you required to? If not I would strongly suggest you use PHP's header() function instead. It works on the server before a page is served and therefore allows for you to forget about what browser the user is using. Its proper use is:

<?php
<php header(\"Location: URLHERE\");
?>

You can only use this before any content has been sent to the browser otherwise you will get an error but it is a solid solution to your problem. Hope this helps some.

-Jim

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

Just a correction on the above PHP code

<?php
header
(\"Location: URLHERE\"); 
?>

Smiling

dk01's picture

He has: 516 posts

Joined: Mar 2002

Woops thanks.

Busy's picture

He has: 6,151 posts

Joined: May 2001

You can use header redirect after logout, just has to be used before any output.
You can always destroy the session on the new page

dk01's picture

He has: 516 posts

Joined: Mar 2002

Yes Busy is correct. Here are some examples of what will and won't work:

Won't work 1:

<?php
session_destroy
(); /* Will be executed properly */
<html>
<
head>
header(\"Location: mypage.php\");  /* This will cause an error since we already started displaying the page! */
</head>
<body>
Hello, this will never be seen and therefore is not needed.
</body>
</html>
?>

Won't work 2:

<?php
header
(\"Location: mypage.php\");  // This will redirect the browser properly.
session_destroy(); // This will never be executed!

<html>
<head>
</head>
<body>
Hello, this will never be seen and therefore is not needed.
</body>
</html>
?>

Will Work:

<?php
session_destroy
(); // This will be executed properly before the redirect.
header(\"Location: mypage.php\");  // This will redirect the browser properly.

<html>
<head>
</head>
<body>
Hello, this will never be seen and therefore is not needed.
</body>
</html>
?>

So all you just need in your page is:

<?php
session_destroy
();
/* Any other php you want here as long as it doesn't output anything */
header(\"Location: mypage.php\");
?>

I hope those example give you an idea of the correct order of things.

-Jim

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.