Tuesday, 17 September 2013

Unable to overwrite/replace existing cookie

Unable to overwrite/replace existing cookie

I am trying to overwrite $_COOKIE['stored_login_token'] with the code
below, but it doesn't seem to work. Basically, I have a table called
'logins' that is storing a userID, token, login date, and token expiration
date. The code below is properly updating the randomly-generated token in
the DB, but is not overwriting the cookie before redirecting to
"network_updates.php"
//generate a random string
function generateRandomString($length = 40) {
$characters =
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, strlen($characters) - 1)];
}
return $randomString;
}
//Login Script
$mysqli2 = new mysqli($db_host,$db_username,$db_password,$db_name);
$login_post_signup_token = generateRandomString();
$expiration_date = date('Y-m-d H:i:s', strtotime('+365 days'));
$suloginid = '';
$stmnt2 = $mysqli2->prepare("INSERT INTO logins (id, user, loginDate,
token, expiration) VALUES (?, ?, ?, ?, ?)");
$stmnt2->bind_param('issss',
$suloginid,
$uuid,
$date,
$login_post_signup_token,
$expiration_date
);
$stmnt2->execute();
$stmnt2->close();
$mysqli2->close();
setcookie(
"stored_login_token",
$login_post_signup_token,
time() + (10 * 365 * 24 * 60 * 60),
'/'
);
header('Location: network_updates.php');
exit();

No comments:

Post a Comment