Php basic tutorial-3 cookie and session

Source: Internet
Author: User
Php basic tutorial-3 cookie and session I. cookie

1. create a cookie. note: The cookie must be sent from the server to the client before any other confidence is sent; otherwise, an error occurs.

Use the function to send a cookie: setcookie (name, value );

2. read cookie

Eg: setcookie ('user', 'trout ');

$ COOKIE ['user'];

3. Add parameters

Set (name, value, expiration, path, domain, sesure, httponly );

Parameter introduction:

Name key, value,

Expiration Time,

Path and domain are restricted to exist in a specific folder or domain,

The sesure value 1 table must use a secure connection. Otherwise, the value 0 table is unnecessary,

Httponly restricts access to cookies, such as disabling Javascript access to cookies.

4. delete a cookie

The same parameter when the cookie is set for the first time. no value is set.

Eg: setcookie ('user', 'Larry ');

Delete: setcookie ('user ','');

Code test: ws. php:

 Setted!

';}?> Test

Choose your preferences:

This is the foot of the document



II. session

1. differences between session and cookie:

1> session stores information on the server and cookie on the client

2> session stores more information

3> safer session

2. create a session. note: it must be called before sending a message to the web.

1> call the function: session_start ();

2> use the array $ _ SESSION to record the value: $ _ SESSION ['email '];

3. access session:

$ _ SESSION ['email '];

4. delete session: the session data exists in two places, so it is deleted from two places:

1> session_start ()

2> unset ($ _ SESSION );

3> session_destory (); // delete

Code test: ws. php:

   
   test   
   name or password is worry!

';}}else {print '

make sure you enter both name and password!

';}}else {print '';}?>

This is the foot of the document

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.