Page 1 of 1

Redirect

Posted: Fri Jul 28, 2006 6:50 am
by subfocus
I want to appologise in advance if this has already been asked, I had a quick search but couldn't find what I was after.

What I'm trying to do is redirect a user on a page to another page automatically, AFTER the MYSQL queries on the page have been executed, similar to - when you login on a forum, it says "thank you for logging in, click here if you are not automatically redirected" etc...

I can't use the following code as the headers have already been sent before my sql code has been run.

Code: Select all

<?php
header('Location: http://www.blah.com/blah.php');
?>
Any help would be great!

Thanks

Posted: Fri Jul 28, 2006 6:57 am
by Jenk

Code: Select all

<?php

//do mysql bits up here..

//send redirect header after
header('Location: http://www.blah.com/blah.php');

?>
the trick is not to output anything if you are going to redirect..

Posted: Fri Jul 28, 2006 6:59 am
by subfocus
Ok, thanks for the quick reply, I'll just get rid of the HTML that was afterwards to let the user know it was doing something :)

Posted: Fri Jul 28, 2006 10:12 am
by pickle
You could do the forward through javascript. Have the forward execute onload in the <body> tag. Then, just don't stop your output until after the queries are done.

Posted: Fri Jul 28, 2006 10:41 am
by RobertGonzalez
You can also use meta redirection in your HTML after the page loads.

Posted: Fri Jul 28, 2006 10:47 am
by Ollie Saunders
Its good practise to "exit;" after a header('Location: ... so that any other code in your script does not execute.

Posted: Fri Jul 28, 2006 12:04 pm
by Jenk
The meta-refresh and Javascript options are also not as appropriate as header() redirects, they damage you SEO rating a bit more iirc.