dannon Posted October 22, 2012 Share Posted October 22, 2012 (edited) I have created a function which inserts a hash into a database and sets a cookie with the same hash. I call this function every time an user visit the site and has the hash cookie set. But when I keep refreshing the page very quickly the cookie hash value and the database hash value go out of sync. As if the function gets terminated in the middle of it processing. What can I do to fix this? I use this code to update the hash: public function updateHash($user_id, $hash) { $query = "INSERT INTO " . TABLE_REMEMBER . " (hash, user_id) VALUES ('$hash', $user_id) ON DUPLICATE KEY UPDATE hash = '$hash'"; $query = $this->db->query($query); if($query){ setcookie(C_HASH, $hash, time() + $this->rememberMeTime, '/'); //stored for a year. } } (I use PDO for my database stuff.) Also... Is there a way to make this query faster? It kind of slows down the generation of my website. Edited October 22, 2012 by dannon Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 22, 2012 Share Posted October 22, 2012 (edited) Have you done some testing to figure out which one holds the newer value and which one holds the older value? My best guess is that the problem is with the cookie. Per the manual Cookies will not become visible until the next loading of a page that the cookie should be visible for. Perhaps the cookie is not done writing before the next page load and you are getting the old value? I wouldn't think so, but it's possible Edited October 22, 2012 by Psycho Quote Link to comment Share on other sites More sharing options...
dannon Posted October 22, 2012 Author Share Posted October 22, 2012 (edited) Have you done some testing to figure out which one holds the newer value and which one holds the older value? My best guess is that the problem is with the cookie. Per the manual Perhaps the cookie is not done writing before the next page load and you are getting the old value? I wouldn't think so, but it's possible Yes I have tested it. The database contained the newer value. Edit:: I'm actually using this code. I have used the code at the top just for testing. $query = "INSERT INTO " . TABLE_REMEMBER . " (hash, user_id) VALUES ('$hash', $user_id) ON DUPLICATE KEY UPDATE hash = '$hash' last = '$last'"; $query = $this->db->query($query); setcookie(C_HASH, $hash, time() + $this->rememberMeTime, '/'); //stored for a year. Edited October 22, 2012 by dannon Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 22, 2012 Share Posted October 22, 2012 Edit:: I'm actually using this code. I have used the code at the top just for testing. $query = "INSERT INTO " . TABLE_REMEMBER . " (hash, user_id) VALUES ('$hash', $user_id) ON DUPLICATE KEY UPDATE hash = '$hash' last = '$last'"; $query = $this->db->query($query); setcookie(C_HASH, $hash, time() + $this->rememberMeTime, '/'); //stored for a year. That's impossible. That query is invalid. But, assuming you had the comma that is needed in there, it really is no different from the first code you posted. Quote Link to comment Share on other sites More sharing options...
dannon Posted October 22, 2012 Author Share Posted October 22, 2012 (edited) That's impossible. That query is invalid. But, assuming you had the comma that is needed in there, it really is no different from the first code you posted. Yea It's without the last = '$last', I forgot to add a comma when I was testing it. So is it possible to fix the cookie problem? Edited October 22, 2012 by dannon Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 23, 2012 Share Posted October 23, 2012 Don't know. We really don't know what the problem is. Could be that the cookie is being read on one page load before it is rewritten on the previous page load since you are doing this so quickly. But, I don't see why you would need to rewrite the cookie on each page load. I would only rewrite it when the user logs in - whether they logged in using a login form or using the hash. That should eliminate the problem completely. Rewriting the hash on each page load has no real benefit that I can see. Quote Link to comment Share on other sites More sharing options...
dannon Posted October 23, 2012 Author Share Posted October 23, 2012 Don't know. We really don't know what the problem is. Could be that the cookie is being read on one page load before it is rewritten on the previous page load since you are doing this so quickly. But, I don't see why you would need to rewrite the cookie on each page load. I would only rewrite it when the user logs in - whether they logged in using a login form or using the hash. That should eliminate the problem completely. Rewriting the hash on each page load has no real benefit that I can see. Oh, doing it whenever the user logs in is a good idea. Why didn't I think of it before? Thank you!! 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.