php基礎教程--3cookie和session

來源:互聯網
上載者:User
一、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:

setted!

';}?> test

Choose your preferences:

This is the foot of the document



二、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:

    test   The session content:' .$SESSION['name']."\n".$SESSION['time'].'';unset($_SESSION);session_destroy();print 'destroy()!';}else {print '

name or password is worry!

';}}else {print '

make sure you enter both name and password!

';}}else {print '';}?>

This is the foot of the document

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.