注意事项,session 储存在服务器端, cookie储存在客户端,cookie需要刷新一次页面才能生效

Cookie 语法

setcookie(string name,string value,int expire,string path,string domain,int secure)

string name cookie名称

string value cookie的值

int expire 到何时结束

string path cookie的作用路径

string domain cookie的作用域

int secure 协议

 
  
  1. setcookie("cookie","cookievalue",time()+3600,"/forum",".域名.com",1)//表示用HTTPS的协议,在域名.com的forum文件夹下,给cookie值为 cookievalue 3600秒  

实例,制作一个简单的登录判断

 
  
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  5. <title>无标题文档</title>  
  6. </head>  
  7.  
  8. <body>  
  9. <?  
  10. /*判断是否从退出登录链接来,如果是,执行清除COOKIE*/ 
  11. if($_GET[type] == "loginout"){  
  12.     setcookie("user","");  
  13.     setcookie("pass","");  
  14.     echo "<script>location.href='cookie.php'</script>";//因为cookie必须是刷新一次才生效所以自动刷新  
  15.     }  
  16. /*判断是否输入了用户名和密码*/ 
  17. if($_POST[user] && $_POST[password])  
  18. {  
  19. setcookie("user",$_POST[user],time()+3600);//保存时间为1小时  
  20. setcookie("pass",$_POST[password],time()+3600);  
  21. echo "<script>location.href='cookie.php'</script>";//因为cookie必须是刷新一次才生效所以自动刷新  
  22. }  
  23. if ($_COOKIE[user] && $_COOKIE[pass])  
  24. {  
  25. /*输出用户名和密码*/ 
  26. echo "用户名:".$_COOKIE[user]."<br>";  
  27. echo "密码:".$_COOKIE[pass]."<br>";  
  28. echo "<a href='cookie.php?type=loginout'>退出登录</a>";  
  29. }  
  30.  
  31.  
  32. ?>  
  33. <form id="login" name="login" method="post" action="">  
  34.   <label for="user"></label>  
  35.   <table width="600" border="1">  
  36.     <tr>  
  37.       <td width="118" align="right">用户名:</td>  
  38.       <td width="466"><label for="user2"></label>  
  39.       <input type="text" name="user" id="user2" /></td>  
  40.     </tr>  
  41.     <tr>  
  42.       <td align="right">密码:</td>  
  43.       <td><label for="password"></label>  
  44.       <input type="text" name="password" id="password" /></td>  
  45.     </tr>  
  46.     <tr>  
  47.       <td align="right">&nbsp;</td>  
  48.       <td><input type="submit" name="sub" id="button" value="登录" /></td>  
  49.     </tr>  
  50.   </table>  
  51. </form>  
  52. </body>  
  53. </html> 

session 语法

必须在输出内容前开启session 使用session_start()函数

$_SESSION[name]=name; 设置session

echo $_SESSION[name]; 取得session并打印

isset($_SESSION[name] ) 判断session

unset( $_SESSION[name]) 删除名为name的session

session_destory()删除所有的session

 实例,制作一个简单的session登录

 
  
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  5. <title>无标题文档</title>  
  6. </head>  
  7.  
  8. <body>  
  9. <?  
  10. session_start();//必须在打印内容之前,也就是程序的最前方使用  
  11. /*判断是否从退出登录链接来,如果是,执行清除session*/ 
  12. if($_GET[type] == "loginout"){  
  13.     unset($_SESSION[user]);  
  14.     unset($_SESSION[pass]);  
  15.       
  16.     }  
  17. /*判断是否输入了用户名和密码*/ 
  18. if($_POST[user] && $_POST[password])  
  19. {  
  20. $_SESSION[user]=$_POST[user];  
  21. $_SESSION[pass]=$_POST[password];  
  22.  
  23. }  
  24. if ($_SESSION[user] && $_SESSION[pass])  
  25. {  
  26. /*输出用户名和密码*/ 
  27. echo "用户名:".$_SESSION[user]."<br>";  
  28. echo "密码:".$_SESSION[pass]."<br>";  
  29. echo "<a href='cookie.php?type=loginout'>退出登录</a>";  
  30. }  
  31.  
  32.  
  33. ?>  
  34. <form id="login" name="login" method="post" action="">  
  35.   <label for="user"></label>  
  36.   <table width="600" border="1">  
  37.     <tr>  
  38.       <td width="118" align="right">用户名:</td>  
  39.       <td width="466"><label for="user2"></label>  
  40.       <input type="text" name="user" id="user2" /></td>  
  41.     </tr>  
  42.     <tr>  
  43.       <td align="right">密码:</td>  
  44.       <td><label for="password"></label>  
  45.       <input type="text" name="password" id="password" /></td>  
  46.     </tr>  
  47.     <tr>  
  48.       <td align="right">&nbsp;</td>  
  49.       <td><input type="submit" name="sub" id="button" value="登录" /></td>  
  50.     </tr>  
  51.   </table>  
  52. </form>  
  53. </body>  
  54. </html>