rghollenbeck Posted October 16, 2013 Share Posted October 16, 2013 (edited) Hello PHP Freaks!I'm coding a new quiz program to keep score of correct and incorrect answers. If I were to just have a server-side "scores.txt", all users would be working off that same file and nobody would know their individual score. So I want to send the user a cookie with an initial score of one million. Each correct answer will be incremented by one thousand and each incorrect answer will be incremented by one. Unless there are over a thousand incorrect answers before a reset (which should never ever happen), this should work.However, the cookie is not setting: <?php // newbie testing new cookie if (isset($_COOKIE['mycookie'])){ $oreos = $_COOKIE['mycookie']; echo $oreos . "<-- cookie set. Where's the data?"; } else { $number_of_hours = 1 ; $date_of_expiration = time() + 60 * 60 * $number_of_hours ; $starting_score = 1000000; setcookie( 'mycookie', $starting_score, $date_of_expiration ); $oreos = $_COOKIE['mycookie']; echo $oreos . "<-- no cookie data. I wonder why this is not setting."; } ?> The IF statement seems to work, but upon the first load and upon multiple reloads, I always get the ELSE results. Does anybody see something that I am missing that should be obvious to me? Or could it be a server setting or browser setting? Something else?Thanks Edited October 16, 2013 by rghollenbeck Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 16, 2013 Share Posted October 16, 2013 When setting cookies you cannot access them immediately after. When you reload the page the cookie data will be available. Quote Link to comment Share on other sites More sharing options...
rghollenbeck Posted October 16, 2013 Author Share Posted October 16, 2013 Thank you. Yes, the page gets reloaded, but the cookie never appears to load. I get this error report: [16-Oct-2013 10:37:31] PHP Warning: Cannot modify header information - headers already sent by (output started at .... (location of my files) ...) So the problem has to be somewhere else. If the cookie was delivered, my program isn't seeing it. Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 16, 2013 Share Posted October 16, 2013 (edited) You cannot use set cookies after you have sent any output to the browser. Quote from 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. Edited October 16, 2013 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
Solution mac_gyver Posted October 16, 2013 Solution Share Posted October 16, 2013 (edited) using a cookie to keep track of a score is going to allow anyone to set their score to anything they want. if you just need to keep track of the information for the duration of a quiz, you would use session variables. if you need to remember the results for all users you would need to store that on the server somehow (storing it in a cookie won't help in this case because the only time you know what the values in cookies are is when the visitor makes a request for your web site.) you can store these in a flat-file database (i.e. text file), by each line in the file having more than one field - a username, the score, ... Edited October 16, 2013 by mac_gyver Quote Link to comment Share on other sites More sharing options...
rghollenbeck Posted October 16, 2013 Author Share Posted October 16, 2013 (edited) Thank you, mac_gyver, for that good idea. I would not care if somebody altered their own scores since it is not for any kind of credit--only for the user's enjoyment, personal growth/development, etc. However, I don't know where this project will lead in the future, and I am looking for the most sensible way of coding it. So, it is back to school for me. I need to learn everything I can about sessions and session variables. That's my homework for today. There are many "down sides" to not being enrolled in school. But the "up side" is that I can learn what I want to learn, such as sessions, and not just what the professor is teaching. Edited October 16, 2013 by rghollenbeck Quote Link to comment Share on other sites More sharing options...
rghollenbeck Posted October 16, 2013 Author Share Posted October 16, 2013 Thanks also to Ch0cu3r. I suppose I could use cookies by constantly having the page reloaded. That would be a mess. I will just use session variables for this. Quote Link to comment 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.