【发布时间】:2021-12-26 23:01:04
【问题描述】:
我有一个使用 mercadopago 付款的网站(类似于 PayPal,来自南美)。
当用户完成付款并被重定向回我的网站时,我会得到一个新的会话 ID,但我无法读取旧的会话 ID,也无法读取之前设置的 cookie。
我的问题是我需要 cookie 或会话值来保持用户登录,如果没有,我需要再次询问用户和密码,而客户端不喜欢这样。
这是我用来设置 cookie 的代码,cmets 解释了我的问题:
<?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
}
?>
【问题讨论】:
标签: php session session-cookies