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.