php使用cookie实现记住用户名和密码实现代码

这篇文章主要介绍了php使用cookie实现记住用户名和密码实现代码,本文直接给出实现代码,需要的朋友可以参考下。

  1. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  2. <form name="form1" method="post" action="check_remember.php">
  3. <table width="300" align="center" cellpadding="0" cellspacing="0">
  4. <thead>
  5. <tr>
  6. <td colspan="2" align="center"><b>记住用户名和密码</b></td>
  7. </tr>
  8. </thead>
  9. <tr align="center">
  10. <td>用 户 名:</td>
  11. <td><input type="text" value="<?php echo $_COOKIE['name'];?>" name="name"></td>
  12. </tr>
  13. <tr align="center">
  14. <td>密码:</td>
  15. <td><input type="password" name="password" value="<?php echo $_COOKIE['password']?>"></td>
  16. </tr>
  17. <tr align="center">
  18. <td>记住用户名和密码</td>
  19. <td><?php if($_COOKIE['remember'] == 1){?><input type="checkbox" name="remember" value="1" checked><?php }else{($_COOKIE['remember'] == "")?><input type="checkbox" name="remember" value="1"><?php }?></td>
  20. </tr>
  21. <tr align="center">
  22. <td colspan="2"><input type="submit" name="Submit" value="提交" /></td>
  23. </tr>
  24. </table>
  25. </form>
  26. check_remember.php
  27. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  28. <?php
  29. $name = $_POST['name'];
  30. $password = $_POST['password'];
  31. $remember = $_POST['remember'];
  32. if($remember == 1){
  33. setcookie('name',$name,time()+3600);
  34. setcookie('password',$password,time()+3600);
  35. setcookie('remember',$remember,time()+3600);
  36. }else{
  37. setcookie('name',$name,time()-3600);
  38. setcookie('password',$password,time()-3600);
  39. setcookie('remember',$remember,time()-3600);
  40. }
  41. echo "<a href=\"remember.php\">返回</a>";
  42. ?>