p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Beginning PHP (http://p2p.wrox.com/forumdisplay.php?f=95)
-   -   PHP ( Cannot modify header information) (http://p2p.wrox.com/showthread.php?t=16766)

squared_ring August 12th, 2004 05:12 AM

PHP ( Cannot modify header information)
hello ppl.. well i was learing to use cookies, but i am stuck cus the below posted script generates

Warning: Cannot modify header information - headers already sent by (output started at D:\xtams\xtams-php\pm.php:1) in D:\xtams\xtams-php\pm.php on line 15
cannot set cookie

error occors at the red indicated lines..

I tried to check if this script is sending ne info or white space to the browser.. but doesnt seem to be.. i mean before setcookie() funcn.

pls help..

$dbhost = 'localhost';
$dbusername = '';
$dbpasswd = '';
$database_name = 'xpm_db';
$conn = mysql_pconnect("$dbhost","$dbusername","$dbpasswd" ) or die ("Couldn't connect to server.");
$db = mysql_select_db("$database_name", $conn) or die("Couldn't select database.");
$sql = "SELECT * FROM users WHERE user='" . $username. "' AND pass='".$password."'";
$result = mysql_query($sql,$conn) or die(mysql_error());
$RecTotal= mysql_num_rows($result);
if ($RecTotal != 0)
setcookie("xpm_user", $row["user"], time()+60, "/", ".localhost.", 0) or die("cannot set cookie");
            Header ("location: \"login.php\"");

Daniel Walker August 12th, 2004 10:12 AM

Check for white space outside of your <?php ?> tags. White space counts as content, so the webserver will wrap a header around it automatically. Then when you try to modify the headers, the PHP engine correctly reports that the headers have benn and gone and been sent, and so lie beyond the realms of modifyability.

Take it easy,

squared_ring August 13th, 2004 02:29 AM

but dan, tat script is my whole file... and it doesnt have ne added html stuffs... ??? :(

Daniel Walker August 13th, 2004 04:22 AM

Yes, but I'm not talking and about a HTML <head /> header: I mean a hypertext MIME header - stuff like:

Expires: Mon, 26 Jul 1997 05:00:00 GMT"
Last-Modified: Monday,11th May 2003 19:06:13 GMT"
Cache-Control: no-cache, must-revalidate"
Pragma: no-cache
Content-type: application/x-msexcel
Content-Disposition: attachment; filename=$file
Content-Description: PHP Generated Data

...the likes of which a webserver wraps around an excel spreadsheet, text file, or any other content, in order to send it across the web. In deed, it is these headers, not the HTML headers that the PHP header directive modifies. Web servers will attempt to automatically wrap appropriate headers around any content they are asked to send. If there is white space before the opening <?php tag in your PHP file, the server will wrap a MIME header around it. Indeed, even if the script was working, the moment you made a call to header('loaction...' the server would have to generate a header on the fly, for you, to hold your header modification.

The error is saying that something has caused the server to generate a header for you already, so you cannot modify it. The most likely cause is whitespace.

Take it easy,

squared_ring August 13th, 2004 05:51 AM

hi again.. well i get exactly what u mean... but the thing is how am i to go bout hunting for that header created ("that irritating white space")....

Daniel Walker August 13th, 2004 06:06 AM

Well, check if there is a newline, space characters, or anythin else before that opening <?php. In other words, the "<" of "<?php" should be on line one, character one of your file.

Whenever you get this error message, this is the first thing to check.

Of course, if it's not that, then I've led you astray, so let me know, and I'll have another go... :)

squared_ring August 14th, 2004 05:29 AM

ATLAST.... got tat white space... :D well thanks a lot dan.. if nething i get back to u if u don mind.. neway thanks once again.....

All times are GMT -4. The time now is 01:34 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.