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.

Edited by dannon
Link to comment
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

Edited by Psycho
Link to comment
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.

Edited by dannon
Link to comment
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
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?

Edited by dannon
Link to comment
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
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
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.