Php + mysql + session login instance program code. In php, we usually use mysqlsession to log on. The following is an example of how to use php + mysql + session to log on to the instance, what we need to learn is that we need to log on to php. we generally achieve this by combining mysql session. The following is an example to introduce the program code for logging on to an instance using php + mysql + session, for more information, see.
Instance
| The code is as follows: |
|
|
// Remove insecure html code for php and mysql.
Function safestrip ($ string ){
$ String = strip_tags ($ string );
$ String = mysql_real_escape_string ($ string );
Return $ string;
}
// Logon information display function
Function messages (){
$ Message = '';
If ($ _ SESSION ['success']! = ''){
$ Message =''
. $ _ SESSION ['success']. '';
$ _ SESSION ['success'] = '';
}
If ($ _ SESSION ['error']! = ''){
$ Message =''
. $ _ SESSION ['error']. '';
$ _ SESSION ['error'] = '';
}
Return $ message;
}
// User logon function
Function login ($ username, $ password ){
// Filter the user name and password entered by the user
$ User = safestrip ($ username );
$ Pass = safestrip ($ password );
// Convert the password to the md5 format
$ Pass = md5 ($ pass );
// Query whether the username and password in the database match
$ SQL =
Mysql_query ("SELECT * FROM user_table WHERE username = '$ user'
AND password = '$ pass' ") or die (mysql_error ());
// If the value is 1, the authentication is successful.
If (mysql_num_rows ($ SQL) = 1 ){
// Start recording in the session
$ _ SESSION ['authorized'] = true;
// Reload the page
$ _ SESSION ['success'] = 'login successful ';
Header ('Location:./index. php ');
Exit;
} Else {
// Log on to the session
$ _ SESSION ['error'] = 'Sorry, the user name or password you entered is incorrect ';
}
}
?> |
The principle is very simple. the user submits the user name and password, and then performs security processing. then, the user goes to mysql for exact comparison. if they are the same, the login will succeed.
Merge sessions are implemented in combination. The following is an example of how to use php + mysql + session to log on to the instance program code. you need to learn this...