我有一個使用 mercadopago 付款的網站(類似于 PayPal,來自南美洲)。
當用戶完成付款并被重定向回我的網站時,我得到一個新的會話 ID,但我無法讀取舊的 ID,也無法讀取之前設定的 cookie。
我的問題是我需要 cookie 或會話值來保持用戶登錄,如果沒有,我需要再次詢問用戶和密碼,而客戶不喜歡那樣。
這是我用來設定 cookie 的代碼,注釋解釋了我的問題:
<?php
session_start();
include("db-connection.php");
if(isset($_SESSION["id_alumno"]))
{
$sid=session_id();
if(isset($_COOKIE["user_token"])){
//just for debbuging
//echo "user_token is a " . $_COOKIE["user_token"];
}else{
//set cookie and update same value in database
setcookie("user_token", $sid, time() 2*24*60*60);
$id_alumno=$_SESSION["id_alumno"];
$sql="UPDATE `alumno` SET `login_token` = '$sid', `login_creado` = NOW() WHERE `alumno`.`id` = '$id_alumno'";
$res=mysqli_query($link, $sql); //this connection values are send in a db-connection.php already included.
}
}else{
$cookie_value=$_COOKIE["user_token"]; // here is my problem, I can't access this value, checking cookie information using chrome and the plugin web developer, I get 2 PHPSESSID (old which was used to set cookie with user_token, and also the user token value, and also this new PHPSESSID)
if(isset($cookie_value)){
$sql="SELECT * FROM alumno where login_token='$cookie_value' and login_token!='no'";
$res=mysqli_query($link, $sql);
if($reg=mysqli_fetch_array($res))
{
//here I can login back the user
}//mysql query
}//if isset cookie value
}
?>
uj5u.com熱心網友回復:
您正在使用session_start()及其默認選項。一旦您離開您的站點,會話 cookie 就會過期。
嘗試手冊中的示例 #3:
<?php
// This sends a persistent cookie that lasts a day.
session_start([
'cookie_lifetime' => 86400,
]);
?>
這會發送一個持續一天的持久性 cookie。
另請參閱:如何在 PHP 中創建持久會話?
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/359071.html
上一篇:每次頁面重新加載時反應獲取資料
