问题描述
我正在建立一个登入网站。我有会话工作正常,但我需要能够让他们登录,如果点击记住我。目前,我有登录脚本保存一个cookie,用户名和密码他们键入到某些cookie。我下面的脚本$ username和$ password在脚本中设置得更高。 $ _SESSION变量设置得很好。
I am building a website with logins. I have sessions working fine but I need to be able to keep them logged in if the click remember me. Currently I have the login script saving a cookie with the Username and Password they type to some cookies. I the below script $username and $password are set higher in the script. The $_SESSION variables are getting set fine. And I know the script is going into the if statement because before I place a alert box in there.
login.php
login.php
while($row = mysql_fetch_array($getLoginResults))
{
$error = "Login Successful";
$_SESSION['Username'] = $_POST['username'];
$_SESSION['Password'] = $_POST['password'];
if($keep == 1)
{
setcookie("Username", $username, time()+3600*24*30);
setcookie("Password", $password, time()+3600*24*30);
}
}
当我检查登录时,所以我knoe的cookie设置,但警报框是空的。
When I check the login, I have a javascript alert so I knoe the cookie is set but the alert box is coming up empty.
check_login.php
check_login.php
echo "<script>alert('".$_COOKIE['Username']."')</script>";
我缺少什么?
推荐答案
尝试使用setcookie与指定的路径,这用于捕获我,因为它假设当前路径默认情况下。使用/将使cookie用于整个域
Try using setcookie with a a path specified, this used to catch me out, as it assumes the current path by default. Using / will make the cookie work for the whole domain
setcookie("Username", $username, time()+3600*24*30, '/');
这篇关于PHP Cookie不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!