Comparison between cookie and session

Source: Internet
Author: User
Nowadays, more and more enterprises and individuals prefer to use the Internet for communication and communication, such as message boards, chat rooms, and forums and blogs.

Nowadays, more and more enterprises and individuals prefer to use the Internet for communication and communication, such as message boards, chat rooms, and forums and blogs.

All of the above methods require the user to register his/her own identity authentication and pass the authentication before they can be used normally, in this way, users can access multiple pages of the website within a certain period of time or jump between different websites smoothly. if this is not done, it will not only cause inconvenience to users, in addition, the user's login is a waste of space during the service period. If all the websites can achieve single-point logon, this can effectively solve this tedious problem, so we use session and cookie in php to solve this tedious problem:

For example, in a simple example

Session_start ();
Include_once 'Conn/conn. php'; // execute the database connection operation
$ Name = addslashes ($ _ POST ['name']); // get the user name
$ Pwd = $ _ POST ['pwd']; // obtain the password
If (! Empty ($ name) and! Empty ($ pwd )){
$ SQL = "select * from tb_member where name = '". $ name. "' and password = '". $ pwd ."'";
$ Num = $ conne-> getRowsNum ($ SQL); // return the query result
$ Conne-> close_rst (); // release the query result
If ($ num = 0 or $ num = '') {// if not
Echo "script" alert ('The user name and password are incorrect! '); Window. location. href = 'index. php'; script ";
} Else {// store the user name data in the Cookie if it is correct
$ Session_id = session_id (); // Obtain the SessionID
Setcookie ("name", $ name, time () + 3600 ,"/");
Setcookie ("id", md5 ($ session_id), time () + 3600 ,"/");
$ Sqls = "insert into tb_login (name, login_id, datetime) values ('". $ name. "','". md5 ($ session_id ). "','". mktime (). "')"; // add data to the database
$ Nums = $ conne-> uidRst ($ sqls); // return the query result
Echo "script alert ('logon successful! '); Window. location. href = 'default. php'; script ";
}
}
?>

Let's take a closer look at the returned results.

Then verify the session:

Session_start ();
Include_once 'Conn/conn. php'; // execute the database connection operation
$ Name = addslashes ($ _ POST ['name']); // get the user name
$ Pwd = $ _ POST ['pwd']; // obtain the password
If (! Empty ($ name) and! Empty ($ pwd )){
$ SQL = "select * from tb_member where name = '". $ name. "' and password = '". $ pwd ."'";
$ Num = $ conne-> getRowsNum ($ SQL); // return the query result
$ Conne-> close_rst (); // release the query result
If ($ num = 0 or $ num = '') {// if not
Echo "script" alert ('The user name and password are incorrect! '); Window. location. href = 'index. php'; script ";
} Else {// store the user name data in the Cookie if it is correct
$ Session_id = session_id (); // Obtain the SessionID
Setcookie ("name", $ name, time () + 3600 ,"/");
Setcookie ("id", md5 ($ session_id), time () + 3600 ,"/");
$ Sqls = "insert into tb_login (name, login_id, datetime) values ('". $ name. "','". md5 ($ session_id ). "','". mktime (). "')"; // add data to the database
$ Nums = $ conne-> uidRst ($ sqls); // return the query result
Echo "script alert ('logon successful! '); Window. location. href = 'default. php'; script ";
}
} Else {
Echo "failed ";
}
?>

The specific operation steps are correct.

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.