Php basic tutorial-3 cookie and session
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:
Setted!';}?>
Test
Choose your preferences:
This is the foot of the document
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:
test
name or password is worry!';}}else {print ' make sure you enter both name and password!
';}}else {print '';}?>This is the foot of the document