我有一个网站,使用梅尔卡多支付(类似于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
}
?>发布于 2021-11-15 19:02:55
您使用的是带有默认选项的session_start()。一旦您离开您的站点,会话cookie就会过期。
尝试手册中的example #3:
<?php
// This sends a persistent cookie that lasts a day.
session_start([
'cookie_lifetime' => 86400,
]);
?>这将发送一个持续一天的持久cookie。
https://stackoverflow.com/questions/69979462
复制相似问题