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)
echo $MyCookie;
echo $CookieArray[0];
echo $_COOKIE["MyCookie"];
echo $HTTP_COOKIE_VARS["MyCookie"];
?>
Copy the code
2) and delete cookies
To delete an existing cookie, there are two ways:
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
session_start(); //Initialize session. Need to be in the file header
$_SESSION[name]=value; //Configure Seeeion
echo $_SESSION[name]; //Use session
isset($_SESSION[name]); //Judge
unset($_SESSION[name]); //Delete
session_destroy(); //Consume all sessions
?>
Copy code
Note: session_register(), session_unregister, session_is_registered are no longer used under php5.
1. Examples of cookie usage
if($_GET['out'])
{ //Used to log out cookies
setcookie('id',"");
setcookie('pass ',"");
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.
}
if($_POST['name']&&$_POST['password']) //If the variables username and password exist, set cookies below
{ //Used Set cookies
setcookie('id',$_POST['name'],time()+3600);
setcookie('pass',$_POST['password'],time()+3600);
echo "< ;script>location.href='login.php'"; //Let cookies take effect in time
}
if($_COOKIE['id']&&$_COOKIE[ 'pass'])
{ //After cookies are set successfully, used to display cookies
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