Front-end small White's daily learning record----PHP (7) Session and Cookie

Source: Internet
Author: User
Tags button type set cookie setcookie

Server-side session technology (communication Protocol)

By subway:
Shenzhen Tong
Get the phone brush.

Cookies (data stored in the client's browser)
Session (data stored in the Web server)

How do I use cookies?

1. Set/unregister cookies (new cookie.php)

<?php/*array (' user ' = ' ghostwu ') *///function Setcookie (c_name,value,expiredays)                      //variable name, value, Expiration days Setcookie ( "User", "Zhangsan");//Set Success if (Isset ($_get[' act ') && $_get[' act '] = = ' logout ') {    //time (): Gets the current    timestamp Setcookie ("User", "", Time ()-1);//Clears the user's value and expires it}?>  <a href= "? act=logout" > Logout </a>

2. Remove the cookie (new cookie2.php)

<?php        /* All cookies inside php are stored in the Super global variable $_cookie        $_cookie is a class array stored in the browser    *    //Print_r ($_cookie) ;    echo $_cookie[' user ';    if (!empty ($_cookie[' user ')) {        echo $_cookie[' user '];    }? >

3. A login page for setting cookies (new login.php)

Use cookie to save username and password when login is successful

Setcookie ("name", $userName); /

Setcookie ("pwd", $USERPWD);

<! DOCTYPE html>

  

4. Use the saved cookie---> Login Success page (new success.php)

<! DOCTYPE html>

How do I use the session?

1. Set/Logoff session(new session. php)

   <?php        /* To use the session, you must first open the session with Session_Start ()               An array of classes is created on the Web server $_session    *    / Session_Start ();    $_session[' name '] = ' zhangsan ';    if (Isset ($_get[' act ')) && $_get[' act '] = = ' Logout '  ) {        //delete an item of an array, or delete a variable        unset ($_session[ ' name ']);    }? ><a href= "? act=logout" > Logout </a>

2. Remove session(new session2.php)

<?php    session_start ();    if (!empty ($_session[' name ')) {        echo $_session[' name '];    }? >

3. One setting session Login page for (new login2.php)

<! DOCTYPE html>

4. Use saved Session ---> Login Success page (new succeed2.php)

<! DOCTYPE html>

5. Save the session ---> registration page (new reg.php) when the registration page is registered successfully

<! DOCTYPE html>

  

  

Front-end small White's daily learning record----PHP (7) Session and 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.