The difference between a session and a cookie

Source: Internet
Author: User

1.session
(1) The session is stored in the server's
(2) Save one copy of each session
(3) The session has a default expiration time
(4) The session can store any type of data
Security, stress on service
Usage:
1. When a page needs to use the session, you need to add session_start () at the top of the page;
2. Operation session
Assignment $_seesion["UID"] = "";
Value $_sessino["UID"]

2.cookie
(1) Cookies are stored on the client's
(2) Each cookie is stored in one copy
(3) cookie has no default expiration time
(4) A cookie can only store strings
Unsafe, no pressure on the server
Usage:
1. Manipulating cookies
Assignment: Setcookie (key,value)
Value: $_cookie["UID"]

Usage:

Assignment value:

<?Phpsession_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 "><HTMLxmlns= "http://www.w3.org/1999/xhtml"><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" /><title>Untitled Document</title></Head><Body><?php $_session["UID"] = "Zhangsan";//Assign value to $_session["UID". ?></Body></HTML>

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.

<?Phpsession_start ();?><!DOCTYPE HTML Public "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/TR/xhtml1/DTD/ Xhtml1-transitional.dtd "><HTMLxmlns= "http://www.w3.org/1999/xhtml"><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" /><title>Untitled Document</title></Head><Body><?phpecho $_session["UID"]?></Body></HTML>

The difference between a session and a cookie

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.