July 25, 201016 yr Hi Im learning about PHP and Im teaching myself from a book. Im on to the section about cookies. This is the code I copied from the book: <?php $user = $_POST['user']; $color = $_POST['color']; $self = $_SERVER['PHP_SELF']; if (($user != null) and ($color != null)) { setcookie( "firstname", $user, time()+36000); setcookie( "fontcolor", $color, time()+36000); header( "Location:getcookie.php"); exit(); } ?> <html><head><title>Set Cookie Data</title></head> <body> <form action="<?php echo($self);?>" method="post"> Please enter your first name: <input type="text" name="user"><br><br> Please type in your favourite font color: <input type="radio" name="color" value="#FF0000">Red <input type="radio" name="color" value="#00FF00">Green <input type="radio" name="color" value="#0000FF">Blue <br><br><input type="submit" value="Submit"> </form> </body> </html> This is the code for the second part: <?php $user = $_COOKIE['firstname']; $color =$_COOKIE['fontcolor']; ?> <html><head><title>Get Cookie Data</title> <style type="text/css"> body{color: <?php echo( $color );?>} </style> </head> <body> <h1>HELLO<?php echo ($user);?>!</h1> </body> </html> The first page works but when input the data and press "Submit" I get this:(Please see attachment for screen shot) can anyone see where I am going wrong?? Thanking you in advance ShedHead.
July 25, 201016 yr Simple ur sending output before u set the cookie no output can be sent before setting a cookie not even html. EDIT: actually i think ur cookies are fine but ur trying to redirect after u set the cookies, the cookies are considered output so ur redirect is gonna flag that error might also be because ur setting one cookie after the other, so both of those suggestions are prob causing the prob
July 25, 201016 yr actually cookies are a part of the HTTP header and if you dont use output buffering the user has been redirected when you send a new Location so you should set cookies first. as the warnings suggest there are errors on each of the 3 Header calls meaning each of them fail, that would suggest that the script starts with a newline prior to the starting PHP tag. (which hasn't been transferred in the example) to explain a HTTP response is split into 2 sections, the HEAD and the BODY the way that you tell the browser that the header has now stopped and the following is the body of the response is by a new line, after that new line the HEAD is closed and you are no longer able to add to the HEAD thus giving you those errors when you try and manipulate the HEAD normally you get issues like this when including some library's due to a newline char at the end php tag, ?>\r\n to solve this PHP allows you to not use the ending php tag so you dont get effected by silly FTP clients (like dreamweaver) adding newlines when they get confused Hope that explains why your getting the error
July 27, 201016 yr Thats fine, but how do I adjust the code in order to make it work? Mmm i've tested ur code and it does work but if its not for u what u could do is use ob_start(); at the top of the page then ob_flush() something like this $user = $_POST['user']; $color = $_POST['color']; $self = $_SERVER['PHP_SELF']; if (($user != null) and ($color != null)) { ob_start(); setcookie( "firstname", $user, time()+36000); setcookie( "fontcolor", $color, time()+36000); header( "Location:getcookie.php"); ob_flush(); exit(); } ?> <html><head><title>Set Cookie Data</title></head> <body> <form action="<?php echo($self);?>" method="post"> Please enter your first name: <input type="text" name="user"><br><br> Please type in your favourite font color: <input type="radio" name="color" value="#FF0000">Red <input type="radio" name="color" value="#00FF00">Green <input type="radio" name="color" value="#0000FF">Blue <br><br><input type="submit" value="Submit"> </form> </body> </html> This is the code for the second part: <?php $user = $_COOKIE['firstname']; $color =$_COOKIE['fontcolor']; ?> <html><head><title>Get Cookie Data</title> <style type="text/css"> body{color: <?php echo( $color );?>} </style> </head> <body> <h1>HELLO<?php echo ($user);?>!</h1> </body> </html>
July 27, 201016 yr hunt for the random newline char at the at the beginning or end of the php tags FILE_START>>>>> <?php /* or */ ?> FILE_END >>> would need to be FILE_START>>>>><?php /* or */ ?>FILE_END >>> output buffering works to if you start it before the new line char in question... you can see there's a newline actually, just noticed it the first error isn't on the first line
August 10, 201016 yr Author Mmm i've tested ur code and it does work but if its not for u what u could do is use ob_start(); at the top of the page then ob_flush() something like this $user = $_POST['user']; $color = $_POST['color']; $self = $_SERVER['PHP_SELF']; if (($user != null) and ($color != null)) { ob_start(); setcookie( "firstname", $user, time()+36000); setcookie( "fontcolor", $color, time()+36000); header( "Location:getcookie.php"); ob_flush(); exit(); } ?> <html><head><title>Set Cookie Data</title></head> <body> <form action="<?php echo($self);?>" method="post"> Please enter your first name: <input type="text" name="user"><br><br> Please type in your favourite font color: <input type="radio" name="color" value="#FF0000">Red <input type="radio" name="color" value="#00FF00">Green <input type="radio" name="color" value="#0000FF">Blue <br><br><input type="submit" value="Submit"> </form> </body> </html> This is the code for the second part: <?php $user = $_COOKIE['firstname']; $color =$_COOKIE['fontcolor']; ?> <html><head><title>Get Cookie Data</title> <style type="text/css"> body{color: <?php echo( $color );?>} </style> </head> <body> <h1>HELLO<?php echo ($user);?>!</h1> </body> </html>
August 10, 201016 yr Author Thanks for that webdesigner93. Ive tried it with the additional code and it works fine.
Create an account or sign in to comment