In many cases, we need to track the activities of visitors on the entire website and perform automatic or semi-automatic identification of their identities (that is, the usual website login and other functions). at this time, we often use cookies and sessions for tracking and judgment.
1. introduction and difference of cookies and Sessions
In many cases, we need to track the activities of visitors on the entire website and perform automatic or semi-automatic identification of their identities (that is, the usual website login and other functions). at this time, we often use cookies and sessions for tracking and judgment.
Differences
Session information is stored on the server, but the session id is stored on the client cookie. of course, the session storage methods of php are diversified, so that even if cookies are disabled, they can be tracked.
Cookies are completely kept on the client side. for example, IE firefox cannot be used when the client disables cookies.
2. Cookie configuration and application
Setcookie (string name, string value, int expire, string path, string domain, int secure );
The name is the identifier of the cookie variable name. in php, you can reference the cookie variable with the same common variable name. Value is the initial value of the cookie variable. expire indicates the effective time of the cookie variable; path indicates the relevant path of the cookie variable; domain indicates the website of the cookie variable; secure is valid only during secure https transmission.
SetCookie ("Cookie", "cookievalue", time () + 3600, "/forum", ".php100.com", 1 );
Receive and process cookies
PHP supports Cookie receiving and processing very well and is completely automatic. it is as simple as the FORM variable principle.
For example, if you set a Cookie named MyCookier, PHP will automatically analyze it from the HTTP header received by the WEB server and form a variable named $ myCookie, which is the same as a common variable, the value of this variable is the Cookie value. Arrays also apply. Another method is to reference the global variable $ HTTP_COOKIE_VARS array of PHP.
Examples are as follows: (assuming these are all set in the previous page and still valid)
Echo $ MyCookie;
Echo $ CookieArray [0];
Echo $ _ COOKIE ["MyCookie"];
Echo $ HTTP_COOKIE_VARS ["MyCookie"];
Delete Cookie
There are two ways to delete an existing Cookie:
1. SetCookie ("Cookie ","");
2. SetCookie ("Cookie", "value", time ()-1/time ());
Cookie usage restrictions
1. it must be set before HTML file content output;
2. different browsers may encounter inconsistent processing of cookies and sometimes incorrect results.
3. restrictions are imposed on the client. A browser can create a maximum of 30 cookies, each of which cannot exceed 4 kB. each WEB site can set a maximum of 20 cookies.
3. Session configuration and application
The code is as follows:
Session_start (); // initialize the session. it must be in the file header.
$ _ SESSION [name] = value; // Configure Seeeion
Echo $ _ SESSION [name]; // use session
Isset ($ _ SESSION [name]); // judge
Unset ($ _ SESSION [name]); // delete
Session_destroy (); // consume all sessions
Note: session_register (), session_unregister, and session_is_registered are no longer used in php5.
// Cookie usage example
The code is as follows:
If ($ _ GET ['out'])
{// Used to cancel cookies
Setcookie ('id ',"");
Setcookie ('pass ',"");
Echo "script" location. href = 'login. php 'script '; // because cookies do not take effect immediately, they only take effect when you refresh again. Therefore, after cancellation, the page is automatically refreshed.
}
If ($ _ POST ['name'] & $ _ POST ['password']) // if the variable username and password exist, set cookies below
{// Used to set cookies
Setcookie ('id', $ _ POST ['name'], time () + 3600 );
Setcookie ('pass', $ _ POST ['password'], time () + 3600 );
Echo "script location. href = 'login. php' script"; // enables cookies to take effect immediately
}
If ($ _ COOKIE ['id'] & $ _ COOKIE ['pass'])
{// When cookies are successfully set, they are used to display cookies
Echo "logon successful!
Username: ". $ _ COOKIE ['id']."
Password: ". $ _ COOKIE ['pass'];
Echo"
";
Echo "cancel cookies"; // double quotation marks. if there are quotation marks, use single quotation marks.
}
?>
// Session usage instance
The code is as follows:
// Session usage instance
Session_start (); // start the session, which must be placed in the first sentence; otherwise, an error occurs.
If ($ _ GET ['out'])
{
Unset ($ _ SESSION ['id']);
Unset ($ _ SESSION ['pass']);
}
If ($ _ POST ['name'] & $ _ POST ['password'])
{
// Set the session
$ _ SESSION ['id'] = $ _ POST ['name'];
$ _ SESSION ['pass'] = $ _ POST ['password'];
}
If ($ _ SESSION ['id'] & $ _ SESSION ['pass'])
{
Echo "logon successful!
User id: ". $ _ SESSION ['id']."
User password: ". $ _ SESSION ['pass'];
Echo"
";
Echo "deregistering a session ";
}
?>