sagnik Posted May 5, 2017 Posted May 5, 2017 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 2And, 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 2I 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.
Krydos Posted May 5, 2017 Posted May 5, 2017 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.
sagnik Posted May 6, 2017 Author Posted May 6, 2017 Sir, here is the register/index.phphttps://pastebin.com/AneB6n3QYou 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.
Krydos Posted May 6, 2017 Posted May 6, 2017 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.
sagnik Posted May 6, 2017 Author Posted May 6, 2017 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.
Krydos Posted May 6, 2017 Posted May 6, 2017 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";
sagnik Posted May 6, 2017 Author Posted May 6, 2017 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
Krydos Posted May 6, 2017 Posted May 6, 2017 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.
sagnik Posted May 6, 2017 Author Posted May 6, 2017 I don't understand how we can see different things as I've uploaded the same I've posted in Pastebin!!
bdistler Posted May 7, 2017 Posted May 7, 2017 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"; }?> -###-
sagnik Posted May 7, 2017 Author Posted May 7, 2017 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.
bdistler Posted May 7, 2017 Posted May 7, 2017 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/ ] ###
sagnik Posted May 7, 2017 Author Posted May 7, 2017 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?
bdistler Posted May 7, 2017 Posted May 7, 2017 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
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now