spikypunker Posted January 26, 2009 Share Posted January 26, 2009 Hey ya, am having a wee problem setting a cookie. I'm using the exact same code as before with no joy, just getting an error saying cannot modify headers? Here is the code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Cookie Setter</title> <?php $Month = 2592000 + time(); //this adds 30 days to the current time setcookie(AboutVisit, date("F jS - g:i a"), $Month); echo "<meta http-equiv=\"refresh\" content=\"0;URL=admin.php\">"; ?> </head> <body> </body> </html> And the error message i get is: Warning: Cannot modify header information - headers already sent by (output started at /home/cookieset.php:7) in /home/cookieset.php on line 10 Seems strange this is being denied when it worked before? Thanks for any help! Chris Quote Link to comment https://forums.phpfreaks.com/topic/142484-solved-php-cookie-prob/ Share on other sites More sharing options...
rhodesa Posted January 26, 2009 Share Posted January 26, 2009 From the PHP Doc http://us2.php.net/setcookie setcookie() defines a cookie to be sent along with the rest of the HTTP headers. Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including <html> and <head> tags as well as any whitespace. Quote Link to comment https://forums.phpfreaks.com/topic/142484-solved-php-cookie-prob/#findComment-746566 Share on other sites More sharing options...
PFMaBiSmAd Posted January 26, 2009 Share Posted January 26, 2009 The only way the code could have worked is if output buffering was turned on in php.ini or a .htaccess file (when available.) Quote Link to comment https://forums.phpfreaks.com/topic/142484-solved-php-cookie-prob/#findComment-746568 Share on other sites More sharing options...
spikypunker Posted January 26, 2009 Author Share Posted January 26, 2009 Yeah i must admit i did read this earlier and thought it must be an old rule because i had this working on a previous prject, but you say it might have been set up on the server? On then well i have a slight follow on question, if this has to be the first thing done in the script, how on earth do i GET a url variable so that i can put this into the cookie? I'm just going to try adjusting the code so it's the first thing. brb Quote Link to comment https://forums.phpfreaks.com/topic/142484-solved-php-cookie-prob/#findComment-746575 Share on other sites More sharing options...
rhodesa Posted January 26, 2009 Share Posted January 26, 2009 so, the point of this script is to set the cookie then forward to admin.php immediately? no HTML is needed for this: <?php $Month = 2592000 + time(); //this adds 30 days to the current time setcookie('AboutVisit', date("F jS - g:i a"), $Month); header('Location: admin.php'); exit; ?> p.s. - note the quotes i put around AboutVisit...you were missing those Quote Link to comment https://forums.phpfreaks.com/topic/142484-solved-php-cookie-prob/#findComment-746589 Share on other sites More sharing options...
Mark Baker Posted January 26, 2009 Share Posted January 26, 2009 On then well i have a slight follow on question, if this has to be the first thing done in the script, how on earth do i GET a url variable so that i can put this into the cookie?No, it has to be done before your script sends any output Quote Link to comment https://forums.phpfreaks.com/topic/142484-solved-php-cookie-prob/#findComment-746590 Share on other sites More sharing options...
spikypunker Posted January 26, 2009 Author Share Posted January 26, 2009 ahhhhh got it working and it GETs the variable and puts em in! Simple! Thanks guys you all ruuule p.s love you Quote Link to comment https://forums.phpfreaks.com/topic/142484-solved-php-cookie-prob/#findComment-746607 Share on other sites More sharing options...
rhodesa Posted January 26, 2009 Share Posted January 26, 2009 p.s love you what...no dinner and a movie first? you sure do get right to the point Quote Link to comment https://forums.phpfreaks.com/topic/142484-solved-php-cookie-prob/#findComment-746611 Share on other sites More sharing options...
premiso Posted January 26, 2009 Share Posted January 26, 2009 p.s love you what...no dinner and a movie first? you sure do get right to the point Bow chicka wow wow! Quote Link to comment https://forums.phpfreaks.com/topic/142484-solved-php-cookie-prob/#findComment-746613 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.