Jump to content

Php Warning: Header Already Sent


sagnik

Recommended Posts

I'm getting a warning in PHP when I'm using session_start().

The warning says that: cannot start session, headers already sent (output started at /home/sgn/public_html/nm/public_html/members/register/index.php:1) in /home/sgn/public_html/nm/public_html/members/register/index.php on line 2

And, Cannot send session cache limiter - headers already sent (output started at /home/sgn/public_html/nm/public_html/members/register/index.php:1) in /home/sgn/public_html/nm/public_html/members/register/index.php on line 2

I couldn't figure out the problem as it says output started at line 1 but in line one has only "<?php" and a unix line break like "<?php[LF]" and in line 2 "session_start()", no spaces, no echo/print.

Link to comment
Share on other sites

The first 2 lines of /home/sgn/public_html/nm/public_html/members/register/index.php are

<?php
require_once $_SERVER['DOCUMENT_ROOT']."/public_html/global/header.php";

So before it does anything else it loads up /home/sgn/public_html/nm/public_html/global/header.php

<?php require_once dirname($_SERVER['DOCUMENT_ROOT'])."/config/config.php";?>
<!DOCTYPE html>

config.php doesn't have any output it looks like, but it also doesn't set your session there. So you need to set your session information before that <!DOCTYPE html> in header.php.

Link to comment
Share on other sites

Sir, here is the register/index.php

https://pastebin.com/AneB6n3Q

You can see that I've written session_start() at line 2 then I've included the header.php in line 3.

And one more thing, when I've copied the text from index.php to paste it in Pastebin, I saw that, a space is added before "<?php" like " <?php" in line 1. I don't understand that, is the space really exist in my code or the space adding when I'm pasting the code as the space is not showing in my code editor (Notepad++). If the space exist then it maybe the problem for the warning.

Link to comment
Share on other sites

So you need to set your session information before that <!DOCTYPE html> in header.php.

If you try to use session commands after you've already printed ANY data to the display then that means the headers are already sent. That's why you're getting that error.
Link to comment
Share on other sites

But I've not started any output before session_start(), here is my first 3 lines:

<?php
session_start();
require_once $_SERVER['DOCUMENT_ROOT']."/global/header.php";
The output starts from line 3.
Link to comment
Share on other sites

cannot start session, headers already sent (output started at /home/sgn/public_html/nm/public_html/members/register/index.php:1)

The first 2 lines of /home/sgn/public_html/nm/public_html/members/register/index.php are

<?php
require_once $_SERVER['DOCUMENT_ROOT']."/public_html/global/header.php";
Link to comment
Share on other sites

Sir, how can you determine it. I've given my whole index.php. You can see the code, I've started the session at line 2 after that I've included header.php

Link to comment
Share on other sites

You know I have root access to the servers, and instead of looking at the code you posted I looked at your actual files that are throwing the error right? Have you edited the files because I can take another look if you have? But I've already seen that you don't set your session headers before printing to the screen.

Link to comment
Share on other sites

I don't understand how we can see different things as I've uploaded the same I've posted in Pastebin!!

Link to comment
Share on other sites

I've given my whole index.php. You can see the code, I've started the session at line 2 after that I've included header.php

-

a BOM signature will cause Headers already sent error in PHP

 

I did a download of your file from Pastebin and found a BOM in it

 

while it is better to use a hex editor - here is PHP code to test your file for a BOM

 

be sure to replace [ theNameOfYourFile.php ] with the name of your file to test

-

<?phperror_reporting(E_ALL); /* replace with your name of file to test */$filename = "theNameOfYourFile.php"; if (is_file($filename) !== TRUE)  {    print "ERROR file ==> " . $filename . " <== not found<br>\n";    EXIT;  } $handle = fopen($filename, "rb"); if ($handle !== NULL)  {    $bom = fread($handle, 3);     $ans = htmlspecialchars($bom);     $hex = bin2hex("$bom");    $hex = chunk_split($hex, 2, ' ');    $hex = trim($hex);     if ($bom == b"\xEF\xBB\xBF")      {        print "is a BOM";      }      else        {          print "is no BOM";        }     print " in file ==> " . $filename . " <==";     print "<br>\n<br>\nfound:<br>\nthis ASCII ==>" . $ans . "<==<br>\n<br>\nthis HEX ==> " . $hex . " <==<br>\n";  }  else    {      print "ERROR file ==> " . $filename . " <== did not open<br>\n";    }?>

-

###

-

 

Link to comment
Share on other sites

Thanks, I think the script will help me a lot to solve this type of problems. As for the problem, I've found that a space was added before "<?php" opening, when I've posted the script in Pastebin.

Link to comment
Share on other sites

I've found that a space was added before "<?php" opening, when I've posted the script in Pastebin.

-

that "space" was the 3 byte BOM

###

 

I see above that you are using [ Notepad++ ] editor

for information about how to remove a BOM see --> [ http://www.larshaendler.com/2015/01/20/remove-bom-with-notepad/ ]

###

Link to comment
Share on other sites

Thanks again, and one more thing, the space was not visible in any code editor, when I've copied and pasted, I've saw that space. Can you tell me why the space was not visible in the editor?

Link to comment
Share on other sites

the BOM - when correctly used - is invisible in your editor

 

the BOM is 3 bytes - hex is [ ef bb bf ] - which are extended ASCII codes - see --> [ http://www.ascii-code.com/ ]

 

for more information about the BOM - see --> [ https://www.w3.org/International/questions/qa-byte-order-mark ]

 

be sure you are using - UTF-8 without BOM - formatting in your editor - when writing PHP scripts

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...