Session 與 Cookie的運用

注意事項,session 儲存在服務器端, cookie儲存在客戶端,cookie須要刷新一次頁面才能生效php

Cookie 語法html

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

string name cookie名稱cookie

string value cookie的值session

int expire 到什麼時候結束dom

string path cookie的做用路徑ide

string domain cookie的做用域函數

int secure 協議post

  
  
  
  
  1. setcookie("cookie","cookievalue",time()+3600,"/forum",".域名.com",1)//表示用HTTPS的協議,在域名.com的forum文件夾下,給cookie值爲 cookievalue 3600秒  

實例,製做一個簡單的登陸判斷ui

  
  
  
  
  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> 
相關文章
相關標籤/搜索