PHP user guide-cookies-PHP user guide-cookies
In this tutorial, we will learn how to use PHP to process cookies. I will try to make things as simple as possible to explain some practical applications of cookies.
What are cookies and their functions?
Cookies are generated by web servers and some client information exists. It is embedded in html information, specified by the server, and transmitted between the client and server
. It is usually used for personalization of user webpages, counters, and storing information of browsed sites.
Cookies and php
Using cookies in PHP is quite easy. You can use the setcookie function to set a cookie. Cookie is part of the HTTP header. Therefore, the cookie setting function must be performed before any content is sent to the browser. This restriction is the same as that of the header () function. Any cookie sent from the client will be automatically converted into a PHP variable. PHP retrieves and analyzes the information header, extracts the cookie name, and changes it to a variable. Therefore, if you set a cookie such as setcookie ("mycookie", "wang"), php will automatically generate a variable named $ mycookie with the value "wang.
Let's first review the setcookie function syntax:
Setcookie (string CookieName, string CookieValue, int CookieExpireTime, path, domain, int secure );
PATH: indicates the directory on the web server, which is the Directory of the called page by default.
DOMAIN: The DOMAIN name that can be used by cookie. the default value is the DOMAIN name of the called page. This domain name must contain two ".", so if you specify your top-level domain name, you must use ".mydomain.com"
SECURE: if it is set to "1", it indicates that the cookie can only be remembered by the user's browser as a SECURE server.
Application:
For a website to be registered, the user's identity will be automatically identified and sent to it for information. if it is a stranger, the user will be told to register first. Follow the information below to create a small data database: first name, last name, email address, and counter ).
Follow these steps to create a table:
Mysql> create database users;
Query OK, 1 row affected (0.06 sec)
Mysql> use users;
Database changed
Mysql> create table info (FirstName varchar (20), LastName varchar (40 ),
Email varchar (40), count varchar (3 ));
Query OK, 0 rows affected (0.05 sec)
Now we have a qualified table. we can create a php page to check cookies against the database.
######################## Index. php ##################################
$ Info = explode ("&", $ Example );
$ FirstName = $ info [0];
$ LastName = $ info [1];
$ Email = $ info [2];
$ Count = $ info [3];
$ Count ++;
$ CookieString = $ FirstName. '&'. $ LastName. '&'. $ email. '&'. $ count;
SetCookie ("Example", $ CookieString, time () + 3600); // set a new cookie
Echo"
Wang example
Hello $ FirstName $ LastName, this is your visit number: $ count
Your email address is: $ email
";
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"
Rafi's Cookie example
Click Here for Site Registration
";
} // End No Cookie instructions
?>
Note: If you are using a remote mysql server or unix server, 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 transmitted in the html header. Remember, php can convert the identifiable cookie to the corresponding variable, so we can check a variable named "Example:
...
} Else {
...
}
If the cookie exists, we add one to the counter and print the user information. if the cookie does not exist, we recommend that you register the cookie first.
If the cookie exists, perform the following steps:
$ 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"
Wang example
Hello $ FirstName $ LastName, this is your visit number: $ count
Your email address is: $ email
";
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 three main parts: first obtain the cookie value, use the explode function to divide it into different variables, increase the counter, and set a new cookie. Then, use html statements to output user information. Finally, update the database with the new counter value.
If this cookie does not exist, the following program will be executed:
Else {// Begin inctructions for no Cookie
Echo"
Rafi's Cookie example
Click Here for Site Registration
";
} // End No Cookie instructions
The following reg. php briefly lists the links to the registration page.
########################### Reg. php #############################
Registering the Site
Registering the site
After all the information is submitted, call another php file to analyze the information.
############################# 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 );
$ R = mysql_fetch_array ($ result );
$ Count = $ r ["count"];
If (isset ($ count )){
$ CookieString = $ FirstName. '&'. $ LastName. '&'. $ email. '&'. $ count;
SetCookie ("Example", $ CookieString, time () + 3600 );
Echo"
User $ FirstName $ LastName already exists. Using the existing
Info.
";
Echo"
Back to Main Page ";
} Else {
$ Count = '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 you for registering.
";
}
} Else {echo "Sorry, some information is missing. Please go back and add all
The information ";}
?>
First, check whether all information is filled in as required. if not, return and 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 in, perform the following:
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"];
If (isset ($ count )){
$ Count ++;
$ CookieString = $ FirstName. '&'. $ LastName. '&'. $ email. '&'. $ count;
SetCookie ("Example", $ CookieString, time () + 3600 );
Echo"
User $ FirstName $ LastName already exists. Using the existing
Info.
";
Echo"
Back to Main Page ";
} 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 you for registering.
";
This program performs several tasks: 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, create a new cookie with the current information. if the same user does not log on to the database, create a new database and create a new cookie.
First, we retrieve user logon 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"];
Check whether there is a counter for this user. use the isset () function
If (isset ($ count )){
...
} Else {
...
}
Add and create a cookie for the counter
$ Count ++; // increase counter
$ CookieString = $ FirstName. '&'. $ LastName. '&'. $ email. '&'. $ count;
SetCookie ("Example", $ CookieString, time () + 3600 );
Echo"
User $ FirstName $ LastName already exists. Using the existing info.
";
Echo"
Back to Main Page ";
If there is no user counter, add a record in mysql and set a cookie
Note: at any time, setcookie is placed before sending any information to the browser; otherwise, an error message is returned.
######################################## #############
--- Advance translation, there is not the correct, please qianjinok@china.com -------