I am trying to store a randomly generated alphanumeric string into a cookie but the value set in the cookie is different from the variable. I would like to know how to store the same value in both the variable and the cookie.
Function to generate the random string
function str_random($len) {
$result = "";
$char = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
$array = str_split($char);
for ($i=0; $i < $len; $i++) {
$randchar = $array[array_rand($array)];
$result .= $randchar;
}
return $result;
}
Storing random string in the cookie
$remember_token = str_random(250);
$dbh->update("users", array(":remember_token" => $remember_token, ), array(":id" => $user[0]["id"]));
setcookie("remember", $remember_token, time() + 60*60*24*7);
Now, if I try to make a var_dump
of the cookie value and the value stored in the database (or the variable), it will return me two different string:
$result = $dbh->find_where("users", array(":id" => $user[0]["id"]));
var_dump($_COOKIE['remember']);
var_dump($remember_token);
var_dump($result[0]['remember_token']);
"Z0PuJEfjZFy87SDJiw2g6Yj1YrRL1xHQ9MDjr53h6OojzauagoRZinuGocxIrtPwQaZkSqyVOtfxBi0WF9VgFvTZIARCSZLyLACvmzCioFnTUtqt0r4vm33XKxcBpqFaWT1HAsKiQiuYVyDXwP9S7Y2VVxMyDxtQ3420MFM9OGmwprtDmYTnEpZ5U9vXl0QpMuLFdPw994OZcOjeg85bj9JUaYyGsTAjQZiEBULay3nPmyssKGQWJlEEtO" string(250)
//for the second line
"J2D1X5GnWgE2T9UX4KtoRque8cqdRaiFifvj5E2r9qNlQoZOLHacatLNKJtIAg1oE51d2Jg8qr14h1a0qK8CjMymk97o23qVVbCcgh3GOPAqJlfU8TXiSZi5cvXmyD2FhXPKZDo2VqwM07GZNaP1JnTnhIcMEEFlLY4XU2JGrGDU66rnv1DuOBWWkwdQlBdzgd4Sbi6laUbNDqIg49s3xRc30su1aS6riZFGPHAlOYWVaLpNnMHBs515o1"
//for both the third line and the fourth line
If I had set the variable $random_token
to a fixed string say foobar
, the cookie would store the same value as the database would:
$remember_token = "foobar";
$$dbh->update("users", array(":remember_token" => $remember_token, ), array(":id" => $user[0]["id"]));
setcookie("remember", $remember_token, time() + 60*60*24*7);
var_dump($remember_token);
var_dump($_COOKIE["remember"]);
Output:
string(6) "foobar"
string(6) "foobar"
I suspect the problem being with the str_random
function, generating a different token for the cookie than the one entered in the database. However, I do not know how to fix it.
For more information, here is the code for the method find_where
and update
:
public function find_where($tablename, $array) {
$req = "SELECT * FROM ". $tablename. " WHERE ". str_replace(":", "", key($array)). "=".key($array);
$smt = $this->conn->prepare($req);
$smt->execute($array);
$result = $smt->fetchAll();
return $result;
}
public function update($tablename, $update, $param) {
$keys_and_values =$this->format_keys_and_values($update);
$req = "UPDATE ". $tablename. " SET ". $keys_and_values. " WHERE ". str_replace(":", "", key($param)). "=". key($param);
$smt = $this->conn->prepare($req);
$execute = array_merge($update, $param);
$smt->execute($execute);
}
The code functions well generally (if I use those functions for another purpose, it would work)
Aucun commentaire:
Enregistrer un commentaire