Implement session and cookie data transfer functions in PHP

Source: Internet
Author: User
1.session Log on the previous page, after a long period of no action, refresh the page will need to log back in.

Features: (1) session is stored in the server;

(2) The session each person (the lander) saves one copy;

(3) The session has a default expiration time (if there are too many people, the data will be too much, due to the storage of the server, will put pressure on the server, over a certain period of time will be cleared out)

(4) The session can store any type of data.

From the above characteristics can be summed up the characteristics of the session is relatively safe . The disadvantage is a certain amount of pressure on the server.

(5) When a page needs to use the session, you need to add session_start () at the top of the page, to write on the page, the front can not appear any code, code refers to the PHP code, you can have HTML code. It is best to get into the habit of <?php session_start ()?> written at the top of the page. A page can be written once.

(6) Operation session. A. Assign value. B. Take the value.

Assignment value:


<?php    session_start ();//session start tag, write at the top.? ><! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

Value:

The value can be in any page value, such as the login page, you can assign the login name, password name and other information to the $_session["UID"], the other pages on the same browser can also take the login name, password name, etc., provided that the assigned page must be opened before, if directly open other page values , you will not be able to fetch it. Then close the browser and then open, you must first open the Assignment page, the other pages to fetch the value.


<?php    session_start ();? ><! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

2.cookie

Features: (1) Cookies are stored on the client, and the browser has a cookie removal option when it clears the browsing information.

(2) The cookie is stored in one copy per person.

(3) The cookie does not have a default expiration time.

(4) Only strings can be stored.

From the above characteristics can be summed up the characteristics of the cookie is not safe, will not cause pressure on the server.

Sessions and cookies are available without special requirements, but when it comes to high security, choose the session.

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.