In this tutorial we will learn how to use PHP to process cookies, and I will try to make things as simple as possible to explain some of the practical applications of cookies.
What is a cookie and how it works?
Cookies are generated by the Web server and have some information about the client. It is embedded in HTML information, specified by the server side, passing information between the client and server side
。 It is usually used to: User Web personalization, counters, store the information of the browsing site, and so on.
Cookies and PHP
Cookies are fairly easy to use in PHP. You can use the Setcookie function to set a cookie. Cookies are part of the HTTP header, so the cookie feature must be set before any content is sent to the browser. This limit is the same as the header () function. Any cookie from the client will automatically be converted into a PHP variable. PHP Gets the information header and analyzes it, extracts the cookie name and becomes a variable. Therefore, if you set cookies such as Setcookie ("MyCookie", "Wang");p HP will automatically produce a variable named "Wang" with a value of $mycookie.
Let's review the Setcookie function syntax first:
Setcookie (String cookiename, string cookievalue, int cookieexpiretime, path, domain, int secure);
PATH: Represents a directory on a Web server, which defaults to the directory where the page is called
The domain name that Domain:cookie can use, default is the domain name of the called page. This domain name must contain two ".", so if you specify your top-level domain, you must use ". MyDomain.com"
Secure: If set to "1", indicates that the cookie can only be remembered by the server that the user's browser considers to be secure
Application:
For a site that needs to be registered, it will automatically identify the user and send it information, if the stranger, will tell him please register first. We create a small database by following the information given below: First name, surname (last name), e-mail address (email addresses), counter (visit counter).
Follow the steps below to build the table:
$CookieString = $FirstName. ' & '. $LastName. ' & '. $email. ' & '. $count;
Setcookie ("Example", $CookieString, Time () +3600); Set a new cookie
echo "<title>wang example</title>
<body>
<p>hello $FirstName $LastName, this is your visit number: $count </p>
<p>your Email address is: $email </p>
<body>
Mysql_connect () or Die ("Problem Connecting to DataBase"); Update DB
$query = "Update info set count= $count where firstname= ' $FirstName ' and
Lastname= ' $LastName ' and email= ' $email ';
$result = Mysql_db_query ("Users", $query) or Die ("Problems .... ");
}//end Existing Cookie instructions
else {//begin inctructions for no Cookie
echo "<title>rafi ' s Cookie example</title>
<body>
<a href= "reg.php" >click here for Site registration</a>
</body>
}//end No Cookie instructions
?>
Note: If you are using a remote MySQL server or UNIX server, you apply the following statement
mysql_connect ("Server", "username", "password") or Die ("Problem Connecting to DataBase");
We want to check whether a cookie with the specified name is sent in the HTML header, and remember that PHP converts the recognizable cookie to the corresponding variable, so we can check for a variable named "Example":
? if (Isset ($Example)) {//begin Instructions for existing Cookie
...
} else {
...
}
If this cookie exists, we will add the counter one and print the user information, if this cookie does not exist, we recommend that the user register first
If the cookie exists, we perform the following steps:
? if (Isset ($Example)) {//begin Instructions for existing Cookie
$info = Explode ("&", $Example); Split the string to variables
$FirstName = $info [0];
$LastName = $info [1];
$email = $info [2];
$count = $info [3];
$count + +;
$CookieString = $FirstName. ' & '. $LastName. ' & '. $email. ' & '. $count;
Setcookie ("Example", $CookieString, Time () +3600); Setting a new cookie
echo "<title>wang example</title>
<body>
<p>hello $FirstName $LastName, this is your visit number: $count </p>
<p>your Email address is: $email </p>
<body>
Mysql_connect () or Die ("Problem Connecting to DataBase"); Update DB
$query = "Update info set count= $count where firstname= ' $FirstName ' and
Lastname= ' $LastName ' and email= ' $email ';
$result = Mysql_db_query ("Users", $query) or Die ("Problems .... ");
}//end Existing Cookie instructions
The above program has 3 main parts: First get the cookie value, use the EXPLODE function into different variables, add the counter, and set a new cookie. The user information is then exported with an HTML statement. Finally, the database is updated with the new counter value.
If this cookie does not exist, the following program will be executed:
else {//begin inctructions for no Cookie
echo "<title>rafi ' s Cookie example</title>
<body>
<a href= "reg.php" >click here for Site registration</a>
</body>
}//end No Cookie instructions
Below reg.php a simple list of links to the registration page
############################ #reg. php#############################
Call another PHP file to parse this information after all the information has been submitted
############################# #reg1. php####################################
?
if ($FirstName and $LastName and $email)
{
Mysql_connect () or Die ("Problem Connecting to DataBase");
$query = "SELECT * from info where firstname= ' $FirstName ' and
Lastname= ' $LastName ' and email= ' $email ';
$result = Mysql_db_query ("Users", $query);
else {echo "Sorry, some information is missing. Please go back and add all
The information "; }
?>
First check that all information is completed as required, if not, return to re-enter
?
if ($FirstName and $LastName and $email)
{
...
else {echo "Sorry, some information is missing. Please go back and add all
The information "; }
?>
If all information is filled out, the following will be performed:
Mysql_connect () or Die ("Problem Connecting to DataBase");
$query = "SELECT * from info where firstname= ' $FirstName ' and
Lastname= ' $LastName ' and email= ' $email ';
$result = Mysql_db_query ("Users", $query);
if (Isset ($count)) {
$count + +;
$CookieString = $FirstName. ' & '. $LastName. ' & '. $email. ' & '. $count;
Setcookie ("Example", $CookieString, Time () +3600);
echo "<p>user $FirstName $LastName already exists. Using the existing
Info.</p> ";
echo "<p><a href=" index.php ">back to Main page</a>";
} else {
$count = ' 1 '; New Visitor-set counter to 1.
$query = "INSERT INTO info values
(' $FirstName ', ' $LastName ', ' $email ', ' $count ') ";
$result = Mysql_db_query ("Users", $query);
$CookieString = $FirstName. ' & '. $LastName. ' & '. $email. ' & '. $count;
Setcookie ("Example", $CookieString, Time () +3600);
echo "Thank for registering.<br>";
This program does a few things: it checks whether the database has such a user (if not, that is, the cookie has been deleted), if so, it specifies the old information and creates a new cookie with the current information, and if the same user does not have a database login, create a database login, and build a new cookie.
First, we retrieve the user login details from the database
Mysql_connect () or Die ("Problem Connecting to DataBase");
$query = "SELECT * from info where firstname= ' $FirstName ' and
Lastname= ' $LastName ' and email= ' $email ';
$result = Mysql_db_query ("Users", $query);
$r =mysql_fetch_array ($result);
$count = $r ["Count"];
Now check if there is a counter for this user, using the Isset () function
if (Isset ($count)) {
...
} else {
...
}
Counter adds and creates a new cookie
$count + +; Increase counter
$CookieString = $FirstName. ' & '. $LastName. ' & '. $email. ' & '. $count;
Setcookie ("Example", $CookieString, Time () +3600);
echo "<p>user $FirstName $LastName already exists. Using the existing info.</p> ";
echo "<p><a href=" index.php ">back to Main page</a>";
If there is no one user counter, add a record in MySQL and set a cookie
Note: At any time, the Setcookie is placed before conveying any data to the browser, otherwise the error message is received.
#####################################################
---Advance translation, there is no place, please qianjinok@china.com-------
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.