Php basic tutorial-3 cookie and session, basic tutorial 3 cookie
I. cookie
1. Create a cookie. Note: The cookie must be sent from the server to the client before any other confidence is sent; otherwise, an error occurs.
Use the function to send a cookie: setcookie (name, value );
2. Read cookie
Eg: setcookie ('user', 'trout ');
$ COOKIE ['user'];
3. Add Parameters
Set (name, value, expiration, path, domain, sesure, httponly );
Parameter introduction:
Name key, value,
Expiration time,
Path and domain are restricted to exist in a specific folder or domain,
The sesure value 1 Table must use a secure connection. Otherwise, the value 0 table is unnecessary,
Httponly restricts access to cookies, such as Disabling Javascript access to cookies.
4. delete a cookie
The same parameter when the cookie is set for the first time. No value is set.
Eg: setcookie ('user', 'Larry ');
Delete: setcookie ('user ','');
Code test: 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">
Ii. session
1. Differences between session and cookie:
1> session stores information on the server and cookie on the client
2> session stores more information
3> safer session
2. Create a session. Note: It must be called before sending a message to the web.
1> call the function: session_start ();
2> use the array $ _ SESSION to record the value: $ _ SESSION ['email '];
3. Access session:
$ _ SESSION ['email '];
4. Delete session: the session data exists in two places, so it is deleted from two places:
1> session_start ()
2> unset ($ _ SESSION );
3> session_destory (); // Delete
Code test: ws. php:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
Test cut chart:
Result: