Page 1 of 1

How to redirect to another page?

Posted: Tue Dec 07, 2004 4:16 am
by Phoenixheart
Yes maybe I'm silly but can anyone tell me how to redirect the browser to another page. We have a response.redirect "anotherpage.asp" in ASP, but in php what is it?

Posted: Tue Dec 07, 2004 5:20 am
by []InTeR[]
You can do it in diferent ways.

I use:

Code: Select all

header("Location: aotherpage.php");

Posted: Tue Dec 07, 2004 7:01 am
by Phoenixheart
I tried it too! Say I have an insert_execute.php that will retreive values from a form in insert_products.htm, and after executing the query, it will then be redirected back to insert_products.htm. I'd rather use php than javascript (history.go(-1) etc.) and I tried to use header("Location:insert_products.htm"); And... it showed an error:
Cannot modify header information - headers already sent by (output started at D:\Saigonartframe Website\insertproducts.php:18)
So what's the problem?

Posted: Tue Dec 07, 2004 7:03 am
by []InTeR[]
You can not have any output before calling the header function.

Not even whitespaces.

Check for that..

Posted: Tue Dec 07, 2004 8:00 am
by Maugrim_The_Reaper
Can also use javascript - it's browser portable and less prone to errors than header(). Also you can use it even after output has been sent without resorting to caching...

Code: Select all

<?php
echo "<script>self.location='http://foo.com/bar.php';</script>";

// Optional notice to user they should enable javascript to use this - it's 
//possible to detect javascript enabled option and use header() if required

echo "<noscript>Javascript is disabled in your browser - click <a href="http://foo.com/bar.php">Here</a> to continue.</noscript>";
?>

Posted: Tue Dec 07, 2004 1:02 pm
by John Cartwright
you may also want to check [php_man]ob_start[/php_man] to get around the header already sent problem