PHP Basic Tutorial--3cookie session

Source: Internet
Author: User
PHP Basic Tutorial--3cookie and session

A. Cookies

1. Create a cookie, note: it must be sent from the server to the client before any other confidence is sent, otherwise an error is caused.

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

2. Read cookies

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

$COOKIE [' User '];

3. Adding parameters

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

Introduction to Parameters:

The name key, value,

expiration existence time,

path and domain restrictions exist in specific folders or domains.

the Sesure value 1 table must use a secure connection, whereas the value 0 table is not necessary,

HttpOnly restricts access to cookies, such as the prohibition of JavaScript access to cookies.

4. Delete Cookies

Use the same parameters when setting cookies for the first time, without setting a value.

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

Delete: Setcookie (' user ', ');

Coding test: ws.php:

 setted!

';}? > <title>Test</title>

Choose Your preferences:

This is the foot of the document


Second, session

1.session differs from Cookie:

1>session store information on the server, cookies are stored on the client

2>session Save More information

3>session More secure

2. Create session, note: must be called before sending information to the Web

1> Call Function: Session_Start ();

2> is recorded by the array $_session: $_session[' email ';

3. Visit session:

$_session[' email '];

4. Deletion of session:session data exists in two places, so it is removed from two places:

1>session_start ()

2>unset ($_session);

3>session_destory (); Delete the server on the

Coding test: ws.php:

   
 
   Test   
 
   The session content: '. $SESSION [' name ']. " \ n ". $SESSION [' time ']. '; Unset ($_session), Session_destroy ();p rint ' Destroy ()! ';} else {print '

Name or password is worry!

';}} else {print '

Make sure you enter both name and password!

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

This is the foot of the document


Test drawing:



Results:


  • 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.