-1

i've used a sample found on online and applied it to my code:

<?php
session_start();
if (isset($_REQUEST["email"]))
{
    $_SESSION["name"] = true;
    $host = $_SERVER["HTTP_HOST"];
    $path = dirname($_SERVER["PHP_SELF"]);
    $sid = session_name() . "=" . session_id();
    header("Location: index.php?$sid");
    exit;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
...
...
and rest of the html code

When I open this page, I got an error:

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /data/server/user/directory/sub-directory/login.php:1) in /data/server/user/directory/sub-directory/login.php on line 2

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /data/server/user/directory/sub-directory/login.php:1) in /data/server/user/directory/sub-directory/login.php on line 2

I looked around to resolve this issue and saw few posts about this in this site also, but I just can't get a good grip on this...can't find the answer.

Please help.

Thanks.

tooepic
  • 3
  • 2
  • possible duplicate of http://stackoverflow.com/questions/1183726/headers-already-sent-in-php, http://stackoverflow.com/questions/1891969/php-headers-already-sent-error, http://stackoverflow.com/questions/2121473/why-am-i-getting-this-php-session-start-error, http://stackoverflow.com/questions/2346827/im-getting-a-php-warning-cannot-modify-header-information-but-i-have-my-includ – outis May 05 '10 at 00:25
  • this has to be the most asked question on SO – Galen May 05 '10 at 00:29
  • You just made me do a double take. – outis May 05 '10 at 00:30
  • 1
    Pasting code you find on the internet is like chewing gum you find laying in the street. :) – Timothy May 05 '10 at 00:42
  • first, i've already said that i looked in this site for this sort of error but can't find the answer...not knowing what BOM meant. that's why i made a post, hoping someone will direct me to right direction....and grossvogel and ZZ coder did. your comments about this post being duplicate or telling me about chewing gum isn't going to help. at one point, all of you did the same...copy/paste and learned your way up. i'm at that stage right now. i'm sorry if this has bothered some of you for whatever the reasons are. and kudos those who are being very helpful. – tooepic May 05 '10 at 01:00

2 Answers2

0

Check for a space, newline, or any other characters before the <?php, Any output before session_start() will cause that error.

The error specifically states that the output is on line 1 of login.php, while session_start() is on line 2. Thus it's probably a space or some other character.

Reece45
  • 2,771
  • 2
  • 17
  • 19
  • i did. that php script is the only script in this html file. i even tried by getting rid of those extra spaces in front of "$_SESSION", "$host", "$path", $sid", "header", and "exit;"...and it still didn't work. – tooepic May 05 '10 at 00:14
  • also, there's no space before or after – tooepic May 05 '10 at 00:16
  • And, if you still find no remedy, you can see if maybe it's a Unicode BOM issue. Check the encoding settings your text editor is using, and select an encoding without BOM, if possible. here's a short explanation of this and other issues:http://bytes.com/topic/php/insights/876324-php-common-newbie-pitfalls-1-headers-already-sent – grossvogel May 05 '10 at 00:19
  • i'm using a simple text editor called notepad. how do i check this encoding settings...or is there such setting availability in notepad? – tooepic May 05 '10 at 00:25
0

NotePad is known to insert BOM in the beginning of the text file. When you save the file, please make sure you don't select Unicode or UTF-8 as encoding.

You can confirm if BOM is inserted by making a hex dump of the file. You can use this utility to do it,

http://www.richpasco.org/utilities/hexdump.html

If it doesn't start with 0x3C, you got BOM.

ZZ Coder
  • 74,484
  • 29
  • 137
  • 169
  • For all these years, i have never noticed about this encoding choice. I just found out and saved it as ANSI and I don't get this warning anymore. Thanks for helping this newbie out. – tooepic May 05 '10 at 00:52