PHP5 Cookie and session usage differences and examples

Source: Internet
Author: User
    1. Setcookie ("Cookie", "Cookievalue", Time () +3600, "/forum", ". jbxue.com", 1);
Copy Code

1), receiving and processing cookiephp support for the reception and processing of cookies is very good, is completely automatic, and the same as the principle of the form variable, especially simple. For example, setting up a cookie,php named Mycookier will automatically parse it from the HTTP header received by the Web server and form a variable like the normal variable, named $ MyCookie, which is the value of the cookie. arrays also apply. Another way is to reference the PHP global variable $http_cookie_vars array. Examples are as follows: (assuming that they were set up in the previous page and still valid)

    1. Echo $MyCookie;
    2. echo $CookieArray [0];
    3. echo $_cookie["MyCookie"];
    4. echo $HTTP _cookie_vars["MyCookie"];
    5. ?>
Copy Code

2), delete cookie to delete an existing cookie, there are two ways:

    1. 1, Setcookie ("Cookie", "");
    2. 2, Setcookie ("Cookie", "Value", Time () -1/time ());
Copy Code

3), the use of the restriction of the cookie 1, must be set before the content output of the HTML file, 2. Different browsers have inconsistent handling of cookies and sometimes have incorrect results. 3, the limit is on the client. A browser can create a maximum of 30 cookies, and each cannot exceed 4KB, and each Web site can set a total of no more than 20 cookies.

3. Session Configuration and Application

    1. Session_Start (); Initializes the session. Need to be in the header of the file
    2. $_session[name]=value; Configure Seeeion
    3. Echo $_session[name]; Use session
    4. Isset ($_session[name]); Judge
    5. Unset ($_session[name]); Delete
    6. Session_destroy ();//Consume all session
    7. ?>
Copy Code

Note: Session_register (), session_unregister,session_is_registered is no longer used under PHP5.

1. Examples of usage of cookies

    1. if ($_get[' out ')

    2. {//For cancelling cookies
    3. Setcookie (' id ', ' ");
    4. Setcookie (' Pass ', ' ");
    5. echo ""; Because cookies are not effective in time, they only take effect when you refresh them again, so the page automatically refreshes when you log off.
    6. }

    7. if ($_post[' name ']&&$_post[' password ')//If the variable username and password exist, set the cookie below

    8. {//For setting cookies
    9. Setcookie (' id ', $_post[' name '],time () +3600);
    10. Setcookie (' Pass ', $_post[' password '],time () +3600);
    11. echo ""; Make cookies effective in time

    12. }

    13. if ($_cookie[' id ']&&$_cookie[' pass ')
    14. {//cookies is used to display cookies after successful setting
    15. echo "Login Successful!
      User name: ". $_cookie[' id ')."
      Password: ". $_cookie[' pass ');
    16. echo "
      ";
    17. echo "Log off cookies"; In double quotation marks, if there is another quotation mark, you need to use single quotation marks.
    18. }
    19. ?>
Copy Code

Copy Code

2. Session Usage Example

    1. Session Usage Example

    2. Session_Start ();//start session, must be placed in the first sentence, otherwise it will be wrong.
    3. if ($_get[' out ')
    4. {

    5. Unset ($_session[' id ');

    6. unset ($_session[' Pass ');
    7. }

    8. if ($_post[' name ']&&$_post[' password '))

    9. {
    10. Used to set session
    11. $_session[' id ']=$_post[' name '];
    12. $_session[' pass ']=$_post[' password ';
    13. }

    14. if ($_session[' id ']&&$_session[' pass ')

    15. {
    16. echo "Login Successful!
      User id: ". $_session[' id ']."
      User password: ". $_session[' pass ');
    17. echo "
      ";
    18. echo "Logout session";
    19. }
    20. ?>
Copy Code
Copy Code
  • 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.