本文介绍了为什么我的Cookie不设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下PHP函数:

  c> 





只有问题,它是空白。任何想法?



UPDATE:
这是函数的调用方式:

  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()语句,以节省一些空间。

解决方案

尝试添加path = /,

 <$ c $ 

c> setcookie('password',$ password,time()+ 60 * 60 * 24 * 365,'/');

还要确保cookie是输出的第一个输出


I have the following PHP function:

function validateUser($username){
    session_regenerate_id ();
    $_SESSION['valid'] = 1;
    $_SESSION['username'] = $username;
    setcookie('username2',$username,time()+60*60*24*365);
    header("Location: ../new.php");
}

And then I fetch the cookie:

echo $_COOKIE['username2']; exit();

(I only put exit() for debugging purposes)

Only problem, it's coming out blank. Any ideas?

UPDATE:This is how the function is called:

    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);

I didn't include all the if() statements to save some space.

解决方案

try adding the path = /, so that the cookie works for the whole site not just the current directory (that has caught me out before)

example

setcookie('password',$password,time()+60*60*24*365, '/');

also make sure the cookie is the first thing being outputas advised in the php manual (this has caught me out before too)

这篇关于为什么我的Cookie不设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 02:58