dongqi8114 2015-01-14 19:29
浏览 77

PHP记住我的Cookie问题

I have a login form with a remember me check box that sets the user email in a cookie that I then want to auto fill in the username the next time they log in. Also, when I use firbug it shows the path of the cookie in the directory where the login.php page is. The index.php is not in the same directory as the login.php directory.

So I have index.php page with the login form. When the user submits the form it calls login.php which has this piece of code:

if (isset($_POST['email'])) {
        if (isset($_POST['remember'])) {
            setcookie("email", $_POST['email'], time() + 9999999);
            $_COOKIE['email'] = $userEmail;
        }
    }

and then on the index.php code I have this code:

$userEmail = $_COOKIE['email'];

and then I use the $userEmail variable to autofill in the username field like this:

<div class="form-group <?php if(isset($userEmail)) { echo $addClass; }?>" >
    <input type="email" name="email" id="email" class="form-control input-lg" placeholder="Email Address" value="<?php if(isset($userEmail)) { echo $userEmail; } ?>">
</div>

I can see that the cookie is being set, but when I try to echo the cookie (email address) it displays nothing.

I know that I am probably not doing this in the best way possible, but it used to work and now it stopped working. Any suggestions on what I am doing wrong?

EDIT: When I use Firebug I see this in the Cookies tab:

  • Name: email
  • Value: the email address
  • Path: this is a different path than the current page i'm on

When I var_dump($_COOKIE['email']) it show NULL

  • 写回答

3条回答 默认 最新

  • douchuang8359 2015-01-14 20:05
    关注
    if (isset($_POST['email'])) {
            if (isset($_POST['remember'])) {
                setcookie("email", $_POST['email'], time() + 9999999);
                $_COOKIE['email'] = $userEmail;
            }
        }
    

    look at the line $_COOKIE['email'] = $userEmail; this should be $userEmail = $_COOKIE['email']; because at that point $userEmail hasn't been given a value so you are essentially setting $_COOKIE['email'] to a null value.

    either that or you need to set $_COOKIE['email'] = $_POST['email'];

    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)