header() not redirecting for some reason...

Reply

Join Date: Nov 2006
Posts: 35
Reputation: boo_lolly is an unknown quantity at this point 
Solved Threads: 1
boo_lolly boo_lolly is offline Offline
Light Poster

header() not redirecting for some reason...

 
0
  #1
Nov 29th, 2006
i'm writing a redirect.php page for a CMS. the admin user makes whatever changes they want to certain information in the database, then clicks submit. they are taken to redirect.php which handles all the sql commands depending on what the admin wanted to change. and then they are redirected to another page, depending on the types of changes they made, as well... the header is not working. here's the error:

Warning: Cannot modify header information - headers already sent by (output started at /../../../admin/redirect.php) in /../../../admin/redirect.php on line 41

[php]
<?php
$uID = $_POST['editID'];
$sub_opt = $_POST['sub_opt'];
$brideFname = $_POST['brideFname'];
$brideLname = $_POST['brideLname'];
$groomFname = $_POST['groomFname'];
$groomLname = $_POST['groomLname'];
$ship_add = $_POST['ship_add'];
$ship_city = $_POST['ship_city'];
$ship_state = $_POST['ship_state'];
$ship_zip = $_POST['ship_zip'];
$_POST['event_month'];
$_POST['event_day'];
$_POST['event_year'];

if($_POST['event_month2'] != NULL){
$event_month = $_POST['event_month2'];
}if($_POST['event_day2'] != NULL){
$event_day = $_POST['event_day2'];
}if($_POST['event_year2'] != NULL){
$event_year = $_POST['event_year2'];
}

if($sub_opt == "cancel"){
echo "<CENTER>All changes were cancelled. You are being redirected to Home.</CENTER>";
$redirect = "admin1.php?action=view_all";
}elseif($sub_opt == "save"){
//open connection
//SQL commands
//close connection
echo "<CENTER>Your changes have been saved. You are being redirected to Home.";
$redirect = "admin1.php?action=view_all";
}elseif($sub_opt == "save_reg"){
//open connection
//same SQL commands
//close connection
echo "Your changes have been saved. You are being redirected to Registry.";
$redirect = "updateRegistry.php";
}

header("Location: $redirect");
?>
[/php]
Last edited by boo_lolly; Nov 29th, 2006 at 4:39 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 348
Reputation: paradox814 is an unknown quantity at this point 
Solved Threads: 4
paradox814's Avatar
paradox814 paradox814 is offline Offline
Posting Whiz

Re: header() not redirecting for some reason...

 
0
  #2
Nov 30th, 2006
if you are going to use a header tag YOU CANNOT output any text/html to the user, which means you need to get rid of all the echo.

when you see this error: Warning: Cannot modify header information - headers already sent by

it means that you've already send data to the user.
comment all the lines that contain echo and you will see your code should work just fine
Last edited by paradox814; Nov 30th, 2006 at 8:35 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 348
Reputation: paradox814 is an unknown quantity at this point 
Solved Threads: 4
paradox814's Avatar
paradox814 paradox814 is offline Offline
Posting Whiz

Re: header() not redirecting for some reason...

 
0
  #3
Nov 30th, 2006
what you could do to fix the problem is put this in the meta tag of the html:
[html]<meta http-equiv="Refresh" content="4;url=http://www.domain.com/link.html">[/html]

you would need to replace the url with $redirect and the 4 seconds with however long you see fit

and it will redirect the user to the new page after 4 seconds
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2
Reputation: silverline0529 is an unknown quantity at this point 
Solved Threads: 0
silverline0529 silverline0529 is offline Offline
Newbie Poster

Re: header() not redirecting for some reason...

 
0
  #4
Dec 1st, 2006
Warning: Cannot modify header information - headers already sent by (output started at /../../../admin/redirect.php) in /../../../admin/redirect.php on line 41

If you are facing this Warning....it means you have some ECHO syntaxes or you may have some waste spaces that are not allowed while using "header" function....Make sure tht u ve removed all ECHOs and waste spaces above the Header.... your function will work fine...
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 1,091
Reputation: MattEvans is a jewel in the rough MattEvans is a jewel in the rough MattEvans is a jewel in the rough 
Solved Threads: 63
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Veteran Poster

Re: header() not redirecting for some reason...

 
0
  #5
Dec 1st, 2006
mmm, the delimiter between a HTTP header and the HTTP response is two line returns (one empty line). you can put aload of stuff before a header, but not an empty line... lol.. PHP tries to handle this automatically (in true CGI you have to send a content-type/location/[some other headers work], otherwise the request fails, if you use echo before you set a header, PHP sends a text/html content-type.)

So, as said, put header() before you do anything that could produce output.

The Location header is a request-level redirect (meaning a page that sends the Location header is never shown); so if you actually want to show something on the page during the redirect, maybe the Redirect header is more appropriate.
Plato forgot the nullahedron..
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 28
Reputation: almualim is an unknown quantity at this point 
Solved Threads: 0
almualim almualim is offline Offline
Light Poster

Re: header() not redirecting for some reason...

 
0
  #6
May 7th, 2008
don't use echo in your code
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 4287 | Replies: 5
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC