timmah1 Posted July 16, 2009 Share Posted July 16, 2009 I just copied this from a tutorial to see how it works, and obviously it don't work or I wouldn't be here Here is the code <?php //Set a cookie which will expire when the browser is closed setcookie('mycookie', 'myvalue'); //Set a cookie which expires in about a year setcookie('longtime', 'nextyear', time() + (60 * 60 * 24 * 365)); ?> <!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>Untitled Document</title> </head> <body> <?php //First we need to check if the user has visited before //Let's use 'visittime' as the name for our cookie if(isset($_COOKIE['visittime'])) { echo 'Welcome back!<br>'; //Calculate how long is since the users last visit //We can do this simply by subtracting the last visit //time from the current time $timeElapsed = time() - $_COOKIE['visittime']; echo 'It has been ' . $timeElapsed . ' seconds since your last visit.'; } else { echo 'I see you are new here'; } //Set the current time as a cookie and make it expire much later. $nextYear = time() + (60 * 60 * 24 * 365); setcookie('visittime', time(), $nextYear); ?> </body> </html> When I go to the page, I get this I see you are new here Warning: Cannot modify header information - headers already sent by (output started at /cookies.php:17) in /cookies.php on line 39 Can anybody tell my why? Also, what is the best way to track a user coming to the site? I tried using Sessions, but since I didn't design this site, something goes wrong with headers already sent by errors, even if I put session_start() at the very top of the page Thanks in advance Link to comment https://forums.phpfreaks.com/topic/166234-solved-probably-stupid-question-about-cookies/ Share on other sites More sharing options...
akitchin Posted July 16, 2009 Share Posted July 16, 2009 you're not allowed to send output to the browser before using a header function, which pretty much any session and cookie functions are. this is in one of the stickies in the PHP Help thread, and is a very common error. the way to fix this would be to move any cookie processing to ABOVE the HTML output in the page, in this case, the "visittime" cookie. chances are the reason you're getting errors even if session_start() is at the very top of the page is that something is still being run before the page you're putting session_start() on. Link to comment https://forums.phpfreaks.com/topic/166234-solved-probably-stupid-question-about-cookies/#findComment-876625 Share on other sites More sharing options...
timmah1 Posted July 16, 2009 Author Share Posted July 16, 2009 I figured it was something simple. Thank you very much for your help Link to comment https://forums.phpfreaks.com/topic/166234-solved-probably-stupid-question-about-cookies/#findComment-876628 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.