Basic php knowledge (4)-cookie usage

Source: Internet
Author: User
Tags php basics
Php Basics (4)-cookie use php Basics-cookie use sample code download page http://xieye.iteye.com/blog/1336095 (in Attachment) cookie simple definition: additional data that the browser sends requests to the server. Co 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 ');

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.