注意事項,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
- setcookie("cookie","cookievalue",time()+3600,"/forum",".域名.com",1)//表示用HTTPS的協議,在域名.com的forum文件夾下,給cookie值爲 cookievalue 3600秒
實例,製做一個簡單的登陸判斷ui
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>無標題文檔</title>
- </head>
- <body>
- <?
- /*判斷是否從退出登陸連接來,若是是,執行清除COOKIE*/
- if($_GET[type] == "loginout"){
- setcookie("user","");
- setcookie("pass","");
- echo "<script>location.href='cookie.php'</script>";//由於cookie必須是刷新一次才生效因此自動刷新
- }
- /*判斷是否輸入了用戶名和密碼*/
- if($_POST[user] && $_POST[password])
- {
- setcookie("user",$_POST[user],time()+3600);//保存時間爲1小時
- setcookie("pass",$_POST[password],time()+3600);
- echo "<script>location.href='cookie.php'</script>";//由於cookie必須是刷新一次才生效因此自動刷新
- }
- if ($_COOKIE[user] && $_COOKIE[pass])
- {
- /*輸出用戶名和密碼*/
- echo "用戶名:".$_COOKIE[user]."<br>";
- echo "密碼:".$_COOKIE[pass]."<br>";
- echo "<a href='cookie.php?type=loginout'>退出登陸</a>";
- }
- ?>
- <form id="login" name="login" method="post" action="">
- <label for="user"></label>
- <table width="600" border="1">
- <tr>
- <td width="118" align="right">用戶名:</td>
- <td width="466"><label for="user2"></label>
- <input type="text" name="user" id="user2" /></td>
- </tr>
- <tr>
- <td align="right">密碼:</td>
- <td><label for="password"></label>
- <input type="text" name="password" id="password" /></td>
- </tr>
- <tr>
- <td align="right"> </td>
- <td><input type="submit" name="sub" id="button" value="登陸" /></td>
- </tr>
- </table>
- </form>
- </body>
- </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登陸
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>無標題文檔</title>
- </head>
- <body>
- <?
- session_start();//必須在打印內容以前,也就是程序的最前方使用
- /*判斷是否從退出登陸連接來,若是是,執行清除session*/
- if($_GET[type] == "loginout"){
- unset($_SESSION[user]);
- unset($_SESSION[pass]);
- }
- /*判斷是否輸入了用戶名和密碼*/
- if($_POST[user] && $_POST[password])
- {
- $_SESSION[user]=$_POST[user];
- $_SESSION[pass]=$_POST[password];
- }
- if ($_SESSION[user] && $_SESSION[pass])
- {
- /*輸出用戶名和密碼*/
- echo "用戶名:".$_SESSION[user]."<br>";
- echo "密碼:".$_SESSION[pass]."<br>";
- echo "<a href='cookie.php?type=loginout'>退出登陸</a>";
- }
- ?>
- <form id="login" name="login" method="post" action="">
- <label for="user"></label>
- <table width="600" border="1">
- <tr>
- <td width="118" align="right">用戶名:</td>
- <td width="466"><label for="user2"></label>
- <input type="text" name="user" id="user2" /></td>
- </tr>
- <tr>
- <td align="right">密碼:</td>
- <td><label for="password"></label>
- <input type="text" name="password" id="password" /></td>
- </tr>
- <tr>
- <td align="right"> </td>
- <td><input type="submit" name="sub" id="button" value="登陸" /></td>
- </tr>
- </table>
- </form>
- </body>
- </html>