Redirect

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
subfocus
Forum Newbie
Posts: 5
Joined: Fri Jul 28, 2006 6:42 am

Redirect

Post 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
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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..
subfocus
Forum Newbie
Posts: 5
Joined: Fri Jul 28, 2006 6:42 am

Post 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 :)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You can also use meta redirection in your HTML after the page loads.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Its good practise to "exit;" after a header('Location: ... so that any other code in your script does not execute.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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.
Post Reply