How to redirect to another page?

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
Phoenixheart
Forum Contributor
Posts: 123
Joined: Tue Nov 16, 2004 7:46 am
Contact:

How to redirect to another page?

Post 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?
[]InTeR[]
Forum Regular
Posts: 416
Joined: Thu Apr 24, 2003 6:51 am
Location: The Netherlands

Post by []InTeR[] »

You can do it in diferent ways.

I use:

Code: Select all

header("Location: aotherpage.php");
Phoenixheart
Forum Contributor
Posts: 123
Joined: Tue Nov 16, 2004 7:46 am
Contact:

Post 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?
[]InTeR[]
Forum Regular
Posts: 416
Joined: Thu Apr 24, 2003 6:51 am
Location: The Netherlands

Post by []InTeR[] »

You can not have any output before calling the header function.

Not even whitespaces.

Check for that..
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post 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>";
?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

you may also want to check [php_man]ob_start[/php_man] to get around the header already sent problem
Post Reply