Jump to content

Cookie Not Setting


Go to solution Solved by mac_gyver,

Recommended Posts

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 by rghollenbeck
Link to comment
https://forums.phpfreaks.com/topic/283022-cookie-not-setting/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/283022-cookie-not-setting/#findComment-1454143
Share on other sites

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 by Ch0cu3r
Link to comment
https://forums.phpfreaks.com/topic/283022-cookie-not-setting/#findComment-1454144
Share on other sites

  • Solution

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 by mac_gyver
Link to comment
https://forums.phpfreaks.com/topic/283022-cookie-not-setting/#findComment-1454145
Share on other sites

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 by rghollenbeck
Link to comment
https://forums.phpfreaks.com/topic/283022-cookie-not-setting/#findComment-1454149
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.