Differences and examples of usage between php5 Cookie and Session

WBOY
Release: 2016-07-25 08:59:39
Original
991 people have browsed it
  1. SetCookie("Cookie", "cookievalue",time()+3600, "/forum", ".jbxue.com", 1);
Copy code

1), receive and process Cookies PHP has very good support for receiving and processing cookies. It is completely automatic and has the same principle as FORM variables. It is very simple. 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 like an ordinary variable named $myCookie. The value of this variable is the value of the Cookie. The same applies to arrays. Another way is to reference PHP's global variable $HTTP_COOKIE_VARS array. Examples are as follows: (assuming these have been set in previous pages and are still valid)

  1. echo $MyCookie;
  2. echo $CookieArray[0];
  3. echo $_COOKIE["MyCookie"];
  4. echo $HTTP_COOKIE_VARS["MyCookie"];
  5. ?>
Copy the code

2) and delete cookies To delete an existing cookie, there are two ways:

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

3) Restrictions on the use of cookies 1. It must be set before the content of the HTML file is output; 2. Different browsers handle cookies inconsistently, and sometimes incorrect results may occur. 3. The restriction is on the client side. The maximum number of cookies that can be created by a browser is 30, and each cookie cannot exceed 4KB. The total number of cookies that can be set by each WEB site cannot exceed 20.

3. Session configuration and application

  1. session_start(); //Initialize session. Need to 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. ?>
Copy code

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

1. Examples of cookie usage

  1. if($_GET['out'])

  2. { //Used to log out cookies
  3. setcookie('id',"");
  4. setcookie('pass ',"");
  5. echo "<script>location.href='login.php'</script>"; //Because cookies do not take effect in time, they will only take effect when you refresh them again, so after logging out Let the page refresh automatically.
  6. }

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

  8. { //Used Set cookies
  9. setcookie('id',$_POST['name'],time()+3600);
  10. setcookie('pass',$_POST['password'],time()+3600);
  11. echo "< ;script>location.href='login.php'"; //Let cookies take effect in time

  12. }

  13. if($_COOKIE['id']&&$_COOKIE[ 'pass'])
  14. { //After cookies are set successfully, used to display cookies
  15. echo "Login successful!
    Username: ".$_COOKIE['id']."
    Password: ".$_COOKIE['pass'];
  16. echo "
    ";
  17. echo "Log out cookies"; //Within double quotation marks, if there are more quotation marks, single quotation marks are required.
  18. }
  19. ?>


  20. Password:
< ;br/>

Copy code

2. Session usage example

  1. //session usage example
  2. session_start();//Start session, must be placed in the first sentence, otherwise an error will occur.
  3. if($_GET['out'])
  4. {

  5. unset($_SESSION['id']);

  6. unset($_SESSION['pass']);
  7. }< /p>
  8. if($_POST['name']&&$_POST['password'])

  9. {
  10. //For setting 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 "< ;br />";
  18. echo "Log out session";
  19. }
  20. ?>
Copy code
  1. User ID:


  2. Password:


Copy code


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template