doutanggun9816
2016-03-27 16:54
浏览 30
已采纳

销毁时登录表单中的cookie错误

I am trying to set cookies to remember the users login details if they tick the 'remember me' box in the login form. I have managed to do this, however when I destroy the cookies by unticking the box, the login fields will both have undefined index errors in them the next time they try and login. How do I get rid of these errors when the cookie has been destroyed? here is the code for the cookies from my login.php:

if(isset($_POST['keep'])){
    setcookie("blarg",$_POST["email"],time()+3600);
    setcookie("bloof",$_POST["password"],time()+3600);
}else{
    setcookie("blarg","",time()-3600);
    setcookie("bloof","",time()-3600);
}

and here is the code to display the cookie data in my login form on my index page:

<input type="text" name="email" id="email" required="required" value="<?php echo $_COOKIE["blarg"] ?>"/>
<br />

<label>Password</label>
<input type="password" name="password" id="password" required="required" value="<?php echo $_COOKIE["bloof"] ?>"/>
<br />

Also this is just for a uni project, so it doesn't matter that the password is stored in a cookie, because I know that would be insecure in the real world.

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • dphfwzn8269 2016-03-27 17:11
    最佳回答

    Check if it exists first:

    <input type="text" 
           name="email" 
           id="email" 
           required="required"
           value="<?php if(isset($_COOKIE["blarg"])) { echo $_COOKIE["blarg"]; } ?>"
    />
    

    Then do same for password field.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题