<?PHP include '../includes/connection.PHP'; date_default_timezone_set('GMT'); $name = $_POST['username']; $pass = $_POST['password']; $query = MysqL_query("SELECT id,username,password FROM users WHERE username = '$name' LIMIT 1"); if(MysqL_num_rows($query) == 0) { echo 'error'; exit; } while($row = MysqL_fetch_array($query)) { if($row['username'] == $name && $row['password'] == $pass) { session_start(); $_SESSION['username'] = $row['username']; $_SESSION['usrID'] = $row['id']; echo 'success'; if($_POST['remember']) { setcookie('username',$row['username'],$exp); setcookie('password',$row['password'],$exp); setcookie('usrID',$row['id'],$exp); } } else { echo 'error'; exit; } } ?>
会话已成功设置,但是该cookie并未设置.我已经尝试设置所有的值(域,路径等),但没有改变任何东西.有没有什么明显的我失踪?
>确保您指定了正确的过期日期格式
>在重定向页面上设置Cookie时,必须在调用头文件(“Location:….”)后设置该cookie;例如:
标题(‘Location:http://www.example.com/’);
setcookie(‘asite’,$site,time()60 * 60,’/’,’site.com’);
>如果你有像www.domain.com/path1/path2/这样的人类网址,那么你必须将cookie路径设置为/适用于所有路径,而不仅仅是当前路径.
setcookie(‘type_id’,$new_type_id,time()60 * 60 * 24 * 30,’/’);
注意参数中的最后一个.
从PHP手册:
The path on the server in which the
cookie will be available on. If set to
‘/’,the cookie will be available
within the entire domain . If set to
‘/foo/’,the cookie will only be
available within the /foo/ directory
and all sub-directories such as
/foo/bar/ of domain . The default
value is the current directory that
the cookie is being set in.
> setcookie()定义要与其余HTTP头一起发送的cookie.像其他标题一样,必须在脚本的任何输出之前发送cookie,这意味着在此之前不应该有任何html / code echo语句.