php cookie注销 设置 输出和注销学习笔记

这是一个朋友在学php时的一个学习笔记,其功能就是设置cookie然后注销及输出cookie值的具体操作方法.

setcookie() 函数用于设置 cookie.

注释:setcookie() 函数必须位于 <html> 标签之前.

语法

setcookie(name, value, expire, path, domain);

实例代码如下:

  1. <?php
  2. //注销cookie
  3. if(isset($_GET['out'])){
  4. setcookie('us',"");
  5. echo "<script>location.href='index.php'</script>";
  6. }
  7. //设置cookie
  8. if(isset($_POST['sub'])){
  9. setcookie("us",$_POST['user'],time()+3600);
  10. echo "<script>location.href='index.php'</script>";
  11. }
  12. //输出和注销cookie
  13. if($_COOKIE['us']){
  14. echo $_COOKIE['us'];
  15. echo "<a href='index.php?out=out'>注销cookies</a>";
  16. }
  17. ?>
  18. <form method="post">
  19. <input type="text" name="user">
  20. <input type="password" name="pass">
  21. <input type="submit" name="sub" value="提交">
  22. </form>