473,586 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3313
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 New Member
You need to put your script like this:
The php<tag> at the first line....

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

Good Luck

http://www.halleswebdesign.com
Nov 15 '05 #3
chipgraphics
8 New Member
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("Locatio n: 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("Locatio n: 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
2962
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 redirecting Warning: Cannot modify header information - headers already sent by (output started at E:\tryphp\direct.php:9) in E:\tryphp\direct.php...
7
11148
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 modify header information - headers already sent by ... The abbreviated code on the login page looks like this: <?php session_start(); ?>
19
7912
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 limiter - headers already sent in /home/httpd/vhosts/monkeyclaus.org/httpdocs/media/audio/pdsIncludes/CommandStartSession.php on line 14
8
2387
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 page where is is to be validated then read into a MySQL table. Once that is done, the form is to send the user either back to the originating page,...
3
1418
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 User_Info_Other where user_id = '1'"); header("Content-type: image/gif"); while ($row = mysql_fetch_array($gotten)) {
10
2354
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 'connectdb.php'; $id = $_GET;
18
2652
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: is supposed to take a fully qualified URL.
0
2108
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 there is input otherwise it changes the template and lists the results on the other template page. That is why there is a $template variable. ====...
2
2169
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 otherwise it re-displays the form request. The $template variable changes the value of which template is displayed. if($mark&&$num){ $mark = $mark =...
0
7912
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7839
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8202
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8216
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5710
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3865
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.