php - Change Cookie w/ Ajax: Strange Delay -


this question has answer here:

i'm coding simple function here. i'm using cookies limit users 5 votes per day (not perfect, in fact pretty easy around, client insisted, no user accounts, next best option).

so, i'm using ajax call in javascript change cookie on fly - every time user clicks 'vote up' button, ajax talks php file reduces cookie value one.

but there's odd delay happening. i've told ajax console.log cookie value (echoed out in php file), , i'm getting delay of 1 value when in log. cookie starts @ 5, when click 'vote' once, should console.log of '4', because value decreased 1 , logged. instead, '5'. next time, should '3', '4', , on.

my code posted below. i'm not sure here - i've thought through code logically, , unless head isn't working, can't see why wouldn't working.

ajax call:

$.ajax({     type: "get",     url: "includes/user_vote.php",     success: function(data){         console.log(data);     },      error: function(jqxhr, textstatus, errorthrown){         //log error if ajax function fails         console.log(textstatus);         console.log(errorthrown);     },      complete: function(data){      } }); 

user_vote.php:

if($_cookie['votes_left'] <= 0){     $newval = 0; } else {     $newval = $_cookie['votes_left'] - 1; }  setcookie('votes_left', $newval, strtotime('today 23:59'), "/"); echo $_cookie['votes_left']; 

there's little more php, not directly related this, posting here anyway. runs on index.php set cookie if isn't set.

if(!isset($_cookie['votes_left'])){      $votes = 5;     $time = date('h:i:s');      setcookie('votes_left', $votes, strtotime('today 23:59'), "/");     setcookie('set_time', $time, strtotime('today 23:59'), "/");  } 

as post link below mentions, cookie super global show value @ time requested page, therefor never see value set to. check post more details

php cookie has 1 refresh delay


Comments

Popular posts from this blog

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -