Php5Cookie and Session usage differences and examples

Source: Internet
Author: User
Php5Cookie and Session usage differences and examples

  1. SetCookie ("Cookie", "cookievalue", time () + 3600, "/forum", ".jbxue.com", 1 );

1) CookiePHP supports Cookie receiving and processing very well and is completely automatic. it is as simple as the FORM variable principle. For example, if you set a Cookie named MyCookier, PHP will automatically analyze it from the HTTP header received by the WEB server and form a variable named $ myCookie, which is the same as a common variable, the value of this variable is the Cookie value. Arrays also apply. Another method is to reference the global variable $ HTTP_COOKIE_VARS array of PHP. Examples are as follows: (assuming these are all set 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. ?>

2) There are two ways to delete an existing Cookie:

  1. 1. SetCookie ("Cookie ","");
  2. 2. SetCookie ("Cookie", "value", time ()-1/time ());

3) restrictions on the use of cookies 1. it must be set before the HTML file content is output; 2. inconsistent processing of cookies by different browsers and sometimes incorrect results may occur. 3. restrictions are imposed on the client. A browser can create a maximum of 30 cookies, each of which cannot exceed 4 kB. each WEB site can set a maximum of 20 cookies.

3,Session configuration and application

  1. Session_start (); // initialize the session. it must be in the file header.
  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 sessions
  7. ?>

Note: session_register (), session_unregister, and session_is_registered are no longer used in php5.

1. cookie usage example

  1. If ($ _ GET ['out'])

  2. {// Used to cancel cookies
  3. Setcookie ('id ',"");
  4. Setcookie ('pass ',"");
  5. Echo "script" location. href = 'login. php 'script '; // because cookies do not take effect immediately, they only take effect when you refresh again. Therefore, after cancellation, the page is automatically refreshed.
  6. }

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

  8. {// Used to set cookies
  9. Setcookie ('id', $ _ POST ['name'], time () + 3600 );
  10. Setcookie ('pass', $ _ POST ['password'], time () + 3600 );
  11. Echo "script location. href = 'login. php' script"; // enables cookies to take effect immediately

  12. }

  13. If ($ _ COOKIE ['id'] & $ _ COOKIE ['pass'])
  14. {// When cookies are successfully set, they are used to display cookies
  15. Echo "logon successful!
    Username: ". $ _ COOKIE ['id']."
    Password: ". $ _ COOKIE ['pass'];
  16. Echo"
    ";
  17. Echo "cancel cookies"; // double quotation marks. if there are quotation marks, use single quotation marks.
  18. }
  19. ?>

2. session usage example

  1. // Session usage instance

  2. Session_start (); // start the session, which must be placed in the first sentence; otherwise, an error occurs.
  3. If ($ _ GET ['out'])
  4. {

  5. Unset ($ _ SESSION ['id']);

  6. Unset ($ _ SESSION ['pass']);
  7. }

  8. If ($ _ POST ['name'] & $ _ POST ['password'])

  9. {
  10. // Set the session
  11. $ _ SESSION ['id'] = $ _ POST ['name'];
  12. $ _ SESSION ['pass'] = $ _ POST ['password'];
  13. }

  14. If ($ _ SESSION ['id'] & $ _ SESSION ['pass'])

  15. {
  16. Echo "logon successful!
    User id: ". $ _ SESSION ['id']."
    User password: ". $ _ SESSION ['pass'];
  17. Echo"
    ";
  18. Echo "deregistering a session ";
  19. }
  20. ?>

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.