473,327 Members | 1,920 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

php redirect: Warning: Cannot modify header information

Does the redirect statement:
header(Location:"http://www.newpage.com");
need to come before certain statements?

I've setup a login page and try to redirect a user once they have
logged in (after I set the appropriate $_SESSION value) but I get the
following error:

Warning: Cannot modify header information - headers already sent by
(...)

If I comment out the header everything works, I just don't get back to
the page I was on when I log in.

Thanks, Greg

Nov 11 '05 #1
3 3296
JDS
On Fri, 11 Nov 2005 12:12:44 -0800, Greg Scharlemann wrote:
Warning: Cannot modify header information - headers already sent by
(...)


Have you *tried* Google?

This is a FAQ.

There is something -- whitespace, probably -- being printed to the browser
before the header() line(s).

header() must be the VERY FIRST thing sent to the browser. (which is not
the same as the first thing in the PHP script).

--
JDS | je*****@example.invalid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

Nov 11 '05 #2
halles
7
You need to put your script like this:
The php<tag> at the first line....

1. <?php>
2. header("Location: http://Your url ");
3.?>

Good Luck

http://www.halleswebdesign.com
Nov 15 '05 #3
You can set session variables and then re-direct a user using the header. You just have to be careful to make sure you DO NOT output any text or anything to the browser prior to sending the header() function. Otherwise it willl result in the error you keep getting "headers already sent".

For example: [color=SeaGreen]THIS IS OK[/color] [PHP]
<?php
session_start();
// Start session and check if field foo was submitted from a form.
// If it was set a session variable to the value the user supplied.

if(isset($_POST['foo'])) {
$_SESSION['bar'] = $_POST['foo'];
} else {
$_SESSION['bar'] = false;
}

// Check if session var is false.
// If it is not false redirect user to the index page.

if(!$_SESSION['bar']) {
header("Location: index.php");
}

?>
[/PHP]

For example: [color=Red]THIS IS NOT OK[/color] [PHP]
<?php
session_start();
// Start session and check if field foo was submitted from a form.
// If it was set a session variable to the value the user supplied.

if(isset($_POST['foo'])) {
$_SESSION['bar'] = $_POST['foo'];
} else {
$_SESSION['bar'] = false;
}

// Check if session var is false.
// ** If it is not false tell user they are being redirected, **
// Then redirect user to the index page.

if(!$_SESSION['bar']) {
/* THIS IS WILL CAUSE AN ERROR */
echo "You are being redirected to the home page.";
header("Location: index.php");
}
?>
[/PHP]

So that's how the cookie crumbles. If you want to dive further into other possibilies you can check out some other php functions using output buffering [color=Gray][[color=Navy]ob_start(), ob_flush(), ob_get_contents()[/color]][/color]. It's a cakewalk once you get the hang of it.

Best of luck

~ Chipgraphics

[color=SeaGreen][color=Black][/color][/color]
Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: soham | last post by:
I want to redirect to another file, isn't this the right code to do so header("Location:http://".$_SERVER.dirname($_SERVER)."/images/$file_name"); the interpreter give me this erorr instead of...
7
by: deko | last post by:
I have a login page that is supposed to redirect the user to his private page after login. But header("Location: $url") does not work after I set the $_SESSION variable - I get "Warning: Cannot...
19
by: lawrence k | last post by:
How can I find out where my script is outputting to the screen for the first time? My error logs are full of stuff like this: PHP Warning: session_start(): Cannot send session cache...
8
by: mcserret | last post by:
I know this is a recurring problem that has been addressed here before, but even after reading all that has gone before, I am still stumped. I have a form that is designed to send data to a PHP...
3
by: gubbachchi | last post by:
Hi, I have written php code to retrive the image from mysql and display it on the webpage. Here is the code <?php require_once("DBConnect.php"); $gotten = @mysql_query("select user_photo from...
10
by: jessica87 | last post by:
hi there, i m using this coding to retrieve the file from database so that my user can download the file... <?php if (!isset($_GET)) die('Invalid Parameter'); include...
18
by: Paul Lautman | last post by:
JRough wrote: What do you mean by "redirect the output to Excel"??? Excel isn't a location, it's a spreadsheet program that some (but not all users) will have on their machine. BTW, Location:...
0
by: JRough | last post by:
I have the following code at the end of a page that lists some query results. It starts in a form that asks the user for input, one of two template pages. One template is a form for user input if...
2
by: JRough | last post by:
I have this code that switches templates depending on if the user fills in a form with a request. The request asks for the $mark & $number. If that request gets input then it displays a list...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.