Basic usage of session and cookie

Source: Internet
Author: User
Session information is stored on the server, but sessionid is stored on the clientcookie. of course, php's session storage methods are diverse, so that even if cookies are disabled, they can be tracked. Introduction to cookies and Sessions

In many cases, we need to track the activities of visitors on the entire website and automatically or semi-automatically identify their identities (that is, the usual website login and other functions). at this time, we often use cookies and sessions for tracking and judgment.

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 you can track the Session even if cookies are disabled.

Cookie is a mechanism for storing data on a remote browser and tracking and identifying users. Cookies are completely kept on the client. for example, IE firefox cannot be used when the client disables cookies.

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.

For example:

SetCookie("Cookie", "cookievalue", time()+3600, "/librarys", ".nowamagic.net", 1);

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"];

2. delete a Cookie

There are two ways to delete an existing Cookie:

  • SetCookie ("Cookie ","");

  • SetCookie ("Cookie", "value", time ()-1/time ());

3. Cookie restrictions

  1. It must be set before HTML file content output;

  2. Different browsers may encounter inconsistent Cookie processing and sometimes incorrect results.

  3. The limitation is 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.

Session configuration and application
Session_start (); // initialize the session. in the file header $ _ SESSION [name] = value; // Configure Seeeionecho $ _ SESSION [name]; // use sessionisset ($ _ 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:

If ($ _ GET ['out']) {// used to cancel the cookie 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 will be automatically refreshed .} If ($ _ POST ['name'] & $ _ POST ['password']) // if the variable username and password exist, set cookies {// for setting cookies setcookie ('id', $ _ POST ['name'], time () + 3600); setcookie ('pass ', $ _ POST ['password'], time () + 3600); echo "script location. href = 'login. php 'script "; // make cookies take effect in a timely manner} if ($ _ COOKIE ['id'] & $ _ COOKIE ['pass']) {// after the cookies are successfully set, it is used to display the 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 example:

 User id: ". $ _ SESSION ['id']."
User password: ". $ _ SESSION ['pass']; echo"
"; Echo" deregistering session ";}?>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.