php基礎教程——3cookie和session,基礎教程3cookie
一、cookie
1.建立cookie,註:必須在發送其他任何信心之前從伺服器發送到用戶端,否則導致錯誤。
使用函數發送cookie: setcookie(name, value);
2.讀取cookie
eg:setcookie('user', 'trout');
$COOKIE['user'];
3.添加參數
set(name, value, expiration, path, domain, sesure, httponly);
參數簡介:
name鍵, value值,
expiration存在時間,
path和 domain限制在特定檔案夾或域中才存在,
sesure值1表必須使用安全連線,反之值0表不必要,
httponly限制對cookie的訪問,比如禁止Javascript對cookie的訪問。
4.刪除cookie
使用通首次設定cookie時相同的參數,不設定值。
eg:setcookie('user', 'larry');
刪除:setcookie('user', '');
編碼測試:ws.php:
<?php if (isset($_POST['submitted'])){setcookie('font-size', $_POST['font_size'], time() + 1000000000, '/', '', 0);setcookie('font-color', $_POST['font_color'], time() + 1000000000, '/', '', 0);$msg = '<p>setted!</p>';}?><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>test</title><base> </head> <body> <?phpif (isset($msg)){print $msg;}?><p>Choose your preferences:</P><form action='ws.php' method='POST'><select name="font_size"><option value=''>Font Size</option><option value='x-small'>x-small</option><option value='x-large'>x-large</option></select><select name="font_color"><option value=''>Font Color</option><option value='999'>Gray</option><option value='0c0'>Green</option></select><input type="submit" name="submit" value="Set My Preferences"/><input type="hidden" name="submitted" value="true"/></form><pre name="code" class="html"><div><p>This is the foot of the document</p></div></body> </html>
二、session
1.session與cookie區別:
1>session將資訊儲存於伺服器,cookie儲存於用戶端
2>session儲存資訊量更大
3>session更安全
2.建立session,註:必須在向web發資訊之前調用
1>調用函數:session_start();
2>通過數組$_SESSION進行數值記錄:$_SESSION[' email '];
3.訪問session:
$_SESSION[' email '];
4.刪除session:session資料存在兩個地方,故從兩個地方刪除:
1>session_start()
2>unset($_SESSION);
3>session_destory(); //刪除伺服器上的
編碼測試:ws.php:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>test</title><base> </head> <body> <?phpdefine('TITLE', 'Login');if (isset($_POST['submitted'])) {if ((!empty($_POST['name'])) && (!empty($_POST['password']))){if ((strtolower($_POST['name']) == 'yf') && ($_POST['password'] == '123456')){// name and password are correct.session_start();$SESSION['name'] = $_POST['name'];$SESSION['time'] = time();print '<h1>The session content:' .$SESSION['name']."\n".$SESSION['time'].'</h1>';unset($_SESSION);session_destroy();print 'destroy()!';}else {print '<p> name or password is worry!</p>';}}else {print '<p> make sure you enter both name and password!</p>';}}else {print '<form action="ws.php" method="post"> <p> Name:<input type="text" name="name" size="20"/></p> <p>Password:<input type="password" name="password" "size="20" /></p> <input type="submit" value="send"> <input type="hidden" name="submitted" value="true"/></form>';}?><div><p>This is the foot of the document</p></div></body> </html>
測試裁圖:
結果: