In PHP5 Cookie and Session are explained in detail

  • 2020-06-01 08:26:27
  • OfStack

1. Introduction and differences of Cookie and Session

In many cases, we need to track the activity of the visitors in the whole website, and carry out automatic or semi-automatic identification of their identity (also known as the functions of website login and so on), at this time, we often use Cookie and Session to track and judge.

The difference between

Session information is stored in server, but session id is stored in client cookie. Of course, php session storage method is diversified, so that if cookie1 is disabled, it can be tracked

Cookie is completely kept on the client side such as: IE firefox can no longer be used when the client side disables cookie

2. Configuration and application of Cookie

Setcookie(string name, string value, int expire,string path, string domain, int secure);
Where name is the cookie variable name identifier, you will be able to use php to reference the cookie variable as if it were a normal variable name. value is the initial value of the cookie variable, and expire is the effective time of the cookie variable. path is the relevant path of the cookie variable; domain represents the website of the cookie variable; secure is only valid when https is securely transmitted.

SetCookie("Cookie", "cookievalue",time()+3600, "/forum", ".php100.com", 1);
Receive and process Cookie
PHP supports the reception and processing of Cookie very well. It is completely automatic, just like the principle of FORM variable 1, which is very simple.
For example, if one Cookie named MyCookier is set, PHP will automatically analyze it from the head of HTTP received by WEB server, and form a variable like ordinary variable 1, named $myCookie, whose value is the value of Cookie. The same goes for arrays. Another option is to reference the PHP global variable $HTTP_COOKIE_VARS array.
Here are some examples :(assuming these were set in the previous page and are still valid)

echo $MyCookie;
echo $CookieArray[0];
echo $_COOKIE["MyCookie"];
echo $HTTP_COOKIE_VARS["MyCookie"];
Delete Cookie

There are two ways to delete an existing Cookie:

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

Restrictions on using Cookie

1. It must be set before the content of HTML file is output;
2. Different browsers handle Cookie differently, and sometimes there will be wrong results.
3. The limitation is on the client side. The maximum number of Cookie that a browser can create is 30, and each cannot exceed 4KB, and the total number of Cookie that can be set per WEB site cannot exceed 20.

3. Configuration and application of Session


session_start();                    // Initialize the session. Need to be in the file header 
$_SESSION[name]=value;  // configuration Seeeion
echo $_SESSION[name];    // use session
isset($_SESSION[name]);   //  judge 
unset($_SESSION[name]);   // delete 
session_destroy() ;              // Consumption of all session

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

//cookies usage example


if($_GET['out'])
{   // For cancellation cookies
    setcookie('id',"");
    setcookie('pass',"");
    echo "<script>location.href='login.php'</script>"; // because cookies It doesn't take effect in time, it only takes effect when you refresh it again, so log out and let the page refresh automatically. 
}
if($_POST['name']&&$_POST['password']) // If the variable username and password exist, set them below cookies
{   // Used to set the cookies
    setcookie('id',$_POST['name'],time()+3600);
    setcookie('pass',$_POST['password'],time()+3600);
    echo "<script>location.href='login.php'</script>"; // let cookies Timely and effective 

}
if($_COOKIE['id']&&$_COOKIE['pass'])
{   //cookies Used for display after successful setup cookies
    echo " Login successful! <br /> User name: ".$_COOKIE['id']."<br/> Password: ".$_COOKIE['pass'];
    echo "<br />";
    echo "<a href='login.php?out=out'> The cancellation cookies</a>";  // Inside the double quotation marks, if there are any more quotation marks, you need to use single quotation marks. 
}
?>
<form action="" method="post">
 The user ID : 
<input type="text" name="name" /><br/><br/>
 Password: 
<input type="password" name="password" /><br/><br />
<input type="submit" name="submit">
</form>

//session usage example


<?php
//session Examples of usage 
session_start();// Start the session Must be put in the first place 1 Sentence, otherwise it will be wrong. 
if($_GET['out'])
{
      
    unset($_SESSION['id']);
    unset($_SESSION['pass']);
}
if($_POST['name']&&$_POST['password'])
{   
   // Used to set the session
    $_SESSION['id']=$_POST['name'];
    $_SESSION['pass']=$_POST['password'];
}
if($_SESSION['id']&&$_SESSION['pass'])
{
    echo " Login successful! <br/> The user ID : ".$_SESSION['id']."<br /> User password: ".$_SESSION['pass'];
    echo "<br />";
    echo "<a href='login.php?out=out'> The cancellation session</a>";
}

?>
<form action="login.php"  method="post">
 The user ID : 
<input type="text" name="name" /><br/><br/>
 Password: 
<input type="password" name="password" /><br/><br />
<input type="submit" name="submit">
</form>


Related articles: