PHP Basic Tutorial--3cookie and session
A. Cookies
1. Create a cookie, note: it must be sent from the server to the client before any other confidence is sent, otherwise an error is caused.
Use the function to send Cookie:setcookie (name, value);
2. Read cookies
Eg:setcookie (' user ', ' trout ');
$COOKIE [' User '];
3. Adding parameters
Set (name, value, expiration, path, domain, sesure, httponly);
Introduction to Parameters:
The name key, value,
expiration existence time,
path and domain restrictions exist in specific folders or domains.
the Sesure value 1 table must use a secure connection, whereas the value 0 table is not necessary,
HttpOnly restricts access to cookies, such as the prohibition of JavaScript access to cookies.
4. Delete Cookies
Use the same parameters when setting cookies for the first time, without setting a value.
Eg:setcookie (' User ', ' Larry ');
Delete: Setcookie (' user ', ');
Coding test: ws.php:
setted!';}? >
<title>Test</title>
Choose Your preferences:
This is the foot of the document
Second, session1.session differs from Cookie:
1>session store information on the server, cookies are stored on the client
2>session Save More information
3>session More secure
2. Create session, note: must be called before sending information to the Web
1> Call Function: Session_Start ();
2> is recorded by the array $_session: $_session[' email ';
3. Visit session:
$_session[' email '];
4. Deletion of session:session data exists in two places, so it is removed from two places:
1>session_start ()
2>unset ($_session);
3>session_destory (); Delete the server on the
Coding test: ws.php:
Test
The session content: '. $SESSION [' name ']. " \ n ". $SESSION [' time ']. '; Unset ($_session), Session_destroy ();p rint ' 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
Test drawing:
Results: