PHP 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
lisamarie_tt
Forum Commoner
Posts: 32
Joined: Fri Jul 15, 2005 8:20 am

PHP REDIRECT

Post by lisamarie_tt »

Here's what i'm trying to do:
SITUATION 1 - On submitting to my page - thePage.php from a form i'd like to:
1. insert some stuff into a database.
2. redirect to another page - aPage.html.

SITUATION 2 - On submitting to thePage.php from a link i'd like to:
1. insert some stuff into a database.
2. display some information and not redirect.

What I've done is passed from the form and from the link a variable to indicate which type of submission it is (note the form submits as get for other reasons). I'm able to insert into the database for both situations but for SITUATION 1 I am unable to redirect.

I've tried the following line:
header("Location: http://localhost/LisaMarie/aPage.html");
but the following error is thrown:
Warning: Cannot modify header information - headers already sent by (output started at C:\htdocs\LisaMarie\thePage.php:83) in C:\htdocs\LisaMarie\thePage.php on line 167

My questions:
1. Why is this error occuring, I don't have any other header setting code?
2. Is there any other way - within php - to perform redirecting.
3. Are there other easily used redirecting code that can be partnered with php to work effectively eg. JavaScript.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

A very nicely formatted post. Unforunantely, this is one of the oft asked questions in PHP. Try: viewtopic.php?t=1157 (although it looks like the formatting has been borked).

About your other questions, you can use HTML META tags for "refresh" to redirect, you can also use JavaScript, although that path is not recommended.

Within PHP? You probably don't have a templating system. If you did, well, then it would be a matter of including the appropriate pages, but that's not recommended.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

The headers get sent as soon as any content appears on the page. So if you'd echo'd anything out, if you've had any whitespace outside of the
<?php tags, etc you'll have sent headers. (There's always at least a Response Header, even if you're not using sessions/cookies.)
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

1. That specific error will be thrown if anything, absolutely anything is printed to the screen prior to the header() function call. You can probably get around this by rethinking when the header() call is made.

2. With PHP you could forget the redirect and just have the page display a different set of content based on the form submission data.

Code: Select all

if (isset($_POST['submit'])){
     //content to display

} else {
    //content to display

}
So all though it isn't a true redirect, the page will look different to the user.

3. You could always window.location() in JS.

Hope that helps.
lisamarie_tt
Forum Commoner
Posts: 32
Joined: Fri Jul 15, 2005 8:20 am

Post by lisamarie_tt »

Thank you all !!!!!!

I was able to immediately figure out the problem. I had some output that was beign displayed before the header statement. (It pays to comment all that you think will not affect general execution :) )

My code works fine now, with the orginal syntax that I used.

Thanks Again for you assistance.
Post Reply