T1000Android Posted October 30, 2011 Share Posted October 30, 2011 Hello all. I am just learning about PHP and the tutorial told me to put the setcookie() and session_start() functions before the <html> tag. The problem is that i get these errors: Warning: Cannot modify header information - headers already sent by (output started at /home/monday/public_html/vlad/PHP in HTML.php:3) in /home/monday/public_html/vlad/PHP in HTML.php on line 4Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/monday/public_html/vlad/PHP in HTML.php:3) in /home/monday/public_html/vlad/PHP in HTML.php on line 8Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/monday/public_html/vlad/PHP in HTML.php:3) in /home/monday/public_html/vlad/PHP in HTML.php on line 8Views=2 Same kind of error i get with the file upload script. Is the problem from my end or from the server end? The server isn't local. Link to comment Share on other sites More sharing options...
Krewe Posted October 30, 2011 Share Posted October 30, 2011 setcookie() does have to be before everything else.session_start() has to be before EVERYTHING on the page, it has to be the VERY FIRST element in the page. So this means if you are starting a MySQL connection before the html, you need to put Session_start() before THAT. Understand? Link to comment Share on other sites More sharing options...
Ingolme Posted October 30, 2011 Share Posted October 30, 2011 setcookie() does have to be before any content on the page because cookies are sent with the headers. Make sure there aren't any spaces, tabs or line breaks before the opening <?php tag, and be sure that you don't use any echo statements or close the <?php ?> block before sending any headers. Link to comment Share on other sites More sharing options...
T1000Android Posted October 30, 2011 Author Share Posted October 30, 2011 The start of the PHP file looks like this: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 strict//EN" "http://www.w3.org/TR/html4/strict.dtd"><?phpsetcookie("user", "me", time()+1800);?><?phpsession_start ();$_SESSION['views']=1;if(isset($_SESSION['views']))$_SESSION['views']=$_SESSION['views']+1;else$_SESSION['views']=1;echo "Views=". $_SESSION['views'];?><html><head> Link to comment Share on other sites More sharing options...
Krewe Posted October 30, 2011 Share Posted October 30, 2011 setcookie() does have to be before any content on the page because cookies are sent with the headers. Make sure there aren't any spaces, tabs or line breaks before the opening <?php tag, and be sure that you don't use any echo statements or close the <?php ?> block before sending any headers. Oh I am sorry, I am still learning obviously and where I was learning it put cookies inside the body area, all around, he rarely ever put them in before everything. Link to comment Share on other sites More sharing options...
Ingolme Posted October 30, 2011 Share Posted October 30, 2011 The start of the PHP file looks like this: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 strict//EN" "http://www.w3.org/TR/html4/strict.dtd"> <?phpsetcookie("user", "me", time()+1800);?> <?phpsession_start (); $_SESSION['views']=1; if(isset($_SESSION['views']))$_SESSION['views']=$_SESSION['views']+1;else$_SESSION['views']=1;echo "Views=". $_SESSION['views'];?> <html><head> When they said before the <html> tag, they really meant before anything at all, even the <!DOCTYPE> declaration. Even before any whitespace characters. Make sure nothing is printed before any headers are sent. Link to comment Share on other sites More sharing options...
T1000Android Posted October 31, 2011 Author Share Posted October 31, 2011 Thanks! This worked: <?php$_SESSION['views']=1;session_start ();setcookie("user", "me", time()+1800);?><?phpif(isset($_SESSION['views']))$_SESSION['views']=$_SESSION['views']+1;else$_SESSION['views']=1;echo "Views=". $_SESSION['views'];?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 strict//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html><head> Link to comment Share on other sites More sharing options...
Ingolme Posted October 31, 2011 Share Posted October 31, 2011 Thanks! This worked: <?php$_SESSION['views']=1;session_start ();setcookie("user", "me", time()+1800);?><?phpif(isset($_SESSION['views']))$_SESSION['views']=$_SESSION['views']+1;else$_SESSION['views']=1;echo "Views=". $_SESSION['views'];?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 strict//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html><head> You could save some bytes and fuse those two PHP blocks. Just remove these two lines:?><?php Link to comment Share on other sites More sharing options...
T1000Android Posted October 31, 2011 Author Share Posted October 31, 2011 Ok ill make a single script. Thanks you have been helpful. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.