Jump to content

Out Of Sync..?


dannon

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/269790-out-of-sync/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/269790-out-of-sync/#findComment-1387083
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/269790-out-of-sync/#findComment-1387084
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/269790-out-of-sync/#findComment-1387088
Share on other sites

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.

:D 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?

Link to comment
https://forums.phpfreaks.com/topic/269790-out-of-sync/#findComment-1387090
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/269790-out-of-sync/#findComment-1387228
Share on other sites

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!!

Link to comment
https://forums.phpfreaks.com/topic/269790-out-of-sync/#findComment-1387281
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.