我有以下的 PHP 函数:
function validateUser($username){
session_regenerate_id ();
$_SESSION['valid'] = 1;
$_SESSION['username'] = $username;
setcookie('username2',$username,time()+60*60*24*365);
header("Location: ../new.php");
}
然后我获取cookie:
echo $_COOKIE['username2']; exit();
(我只是为了调试目的加入了exit()
)
唯一的问题是它输出为空白。有任何想法吗?
更新:这是函数的调用方式:
if(mysql_num_rows($queryreg) != 0){
$row = mysql_fetch_array($queryreg,MYSQL_ASSOC);
$hash = hash('sha256', $row['salt'] . hash('sha256', $password));
if($hash == $row['password']) {
if($row['confirm'] == 1){
if(isset($remember)){
setcookie('username',$username,time()+60*60*24*365);
setcookie('password',$password,time()+60*60*24*365);
} else {
setcookie('username','',time()-3600);
setcookie('password','',time()-3600);
}
validateUser($username);
为了节省一些空间,我没有包含所有的if()
语句。
new.php
上。 - Jason$username
包含输入的用户名。 - Jason$username
有一个值,而print_r($_COOKIE);
没有显示正确的 cookie 痕迹。 - Jasonsetcookie('username2',$ username,time()+60 * 60 * 24 * 365,'/');
,因为 cookie 必须在所有路径上都可用,而不仅仅是当前路径。 - Jason