Php + mysql + session login instance program code _ PHP Tutorial

Source: Internet
Author: User
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...

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.