Basic php knowledge (4)-basic php Usage of cookies-xieye on the download page of cookie sample code. iteye. comblog1336095 (in the attachment) cookie is a simple definition: additional data transmitted when the browser sends a request to the server. Cookie Principle: The web protocol stipulates that a small amount of data can be stored on the browser client when the server returns a request. the browser will request information from the same domain name server next time. php Basics (4)-cookie usage
Basic php knowledge-cookie usage
Sample Code Download page http://xieye.iteye.com/blog/1336095 (in attachment)
Simple Definition of cookie: additional data sent when the browser sends a request to the server.
Cookie Principle: The web protocol stipulates that a small amount of data can be stored on the browser client when the server returns a request. the browser will
At the same time, this section of information is automatically sent to the server, which is transparent to users, users do not need to understand these things can access the Internet as usual.
One feature of cookie is that the time parameter can be included when the server saves the client. the browser determines whether the cookie expires. if the cookie expires
Not to the server.
If the server does not process the cookie, each time the browser sends a request to the server, it will automatically send the cookie with the same content.
Another feature of cookie is the combination of multiple names and values.
Example: automatic logon.
There are 3 pages, one home page and one login page.
You must first add a user
Insert into test. member (username, password) values ('admin', md5 ('1 '));
9. php, 10.php, and 11quit. php are examples of automatic logon failures.
19. php, login php, and 21quit. php are examples of automatic logon.
Demo
The first is a common logon example.
Http: // localhost/command/peixun/cookie/9.php, prompting you to log on
Enter 10.php, enter admin, and 1 to jump to 9.php. the current user name is displayed. you can exit if you want.
If you do not click "exit", close the browser and enter http: // localhost/command/peixun/cookie/9.phpin the browser.
Next is an example of automatic login using cookies.
Http: // localhost/command/peixun/cookie/19.php, prompting you to log on
The 10. php check box is displayed. if you do not select the check box, the result is exactly the same as that in the preceding example. enter admin and 1 to jump to 19.php, and the current user name is displayed. you can exit if you want.
If admin is selected, 1 is redirected to 19.php. you cannot click the "exit" link to close the browser.
Double-click the browser icon on the desktop to open the browser again and find that the browser is logged on. this is the cookie effect.
Important: This example is only used to demonstrate the usage of cookies. for the sake of security, it is best to hide the meaning of the cooikie field and the program needs to be modified.
The code can be downloaded. only the code of the 2nd examples is displayed.
19. php
Select (); $ select-> from ('test. member', 'Count (*) ')-> where ('username =? ', $ Username)-> where ('password =? ', $ Password); $ result = $ db-> fetchOne ($ select); if ($ result) {// For $ _ SESSION ['islogin'] = 1; $ _ SESSION ['username'] = $ username; $ html. = 'Welcome ,'. $ _ SESSION ['username']. "Exit" ;}}} if (! (Isset ($ _ SESSION ['islogin']) & $ _ SESSION ['islogin'] = 1) {$ html. = 'Log onto ';} $ html. ="
This is the homepage "; echo $ html;
20. php
Longs; echo $ html;} else {// echo $ _ POST ['name1']; $ username =$ _ POST ['username']; $ password = $ _ POST ['password']; $ db = Sys: getdb (); $ select = $ db-> select (); $ select-> from ('test. member ', 'Count (*)')-> where ('username =? ', $ Username)-> where ('password =? ', Md5 ($ password); $ result = $ db-> fetchOne ($ select); if ($ result) {$ _ SESSION ['islogin'] = 1; $ _ SESSION ['username'] = $ username; $ time = time () + 3600*24*365; setcookie ('rememberme ', intval ($ _ POST ['memberme']), $ time, "/"); setcookie ('password', md5 ($ password), $ time ,"/"); setcookie ('username', $ username, $ time, "/"); header ('Location: 19. php ');} else {$ html = <
Enter the correct user name and password. Longs; echo $ html ;}}
21quit. php
$ Ko) {if ($ key! = 'Phpsessid') {setcookie ($ key, "", $ date, "/"); // method for deleting a cookie} header ('Location: 19. php ');