Home  >  Article  >  Backend Development  >  php+mysql+session login example program code_PHP tutorial

php+mysql+session login example program code_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:16:09860browse

When we want to log in in php, we generally implement it by combining mysql session. Below I will give an example to introduce the php+mysql+session login example program code. Friends who need to learn can refer to it.

Example

// Remove unsafe html code for php and mysql.
function safestrip($string){
$string = strip_tags($string);
$string = mysql_real_escape_string($string);
Return $string;
}

//Login 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 login function
function login($username, $password){

//Filter the username and password entered by the user
$user = safestrip($username);
$pass = safestrip($password);

//Convert password to 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=1, it means the authentication is successful
if (mysql_num_rows($sql) == 1) {

//Start recording in session
               $_SESSION['authorized'] = true;

                                                                                                  // Reload the page
                $_SESSION['success'] = 'Login successful';
               header('Location: ./index.php');
exit;

} else {
// Login failure is recorded in session
          $_SESSION['error'] = 'Sorry, the username or password you entered is incorrect';
}
}
?>
The code is as follows
 代码如下 复制代码

// 为php和mysql剔除不安全html代码。
function safestrip($string){
$string = strip_tags($string);
$string = mysql_real_escape_string($string);
return $string;
}

//登录信息显示函数
function messages() {
$message = '';
if($_SESSION['success'] != '') {
$message = ''
   .$_SESSION['success'].'
';
   $_SESSION['success'] = '';
 }
 if($_SESSION['error'] != '') {
   $message = ''
   .$_SESSION['error'].'
';
   $_SESSION['error'] = '';
 }
 return $message;
}
 
// 用户登录函数
function login($username, $password){
 
//过滤用户输入的用户名和密码
$user = safestrip($username);
$pass = safestrip($password);
 
//将密码转换为md5格式
$pass = md5($pass);
 
 // 查询数据库中用户名和密码是否匹配
 $sql =
 mysql_query("SELECT * FROM user_table WHERE username = '$user'
 AND password = '$pass'")or die(mysql_error());
 
 //如果=1则表示认证成功
 if (mysql_num_rows($sql) == 1) {
 
             //开始记录在session中
             $_SESSION['authorized'] = true;
 
             // 重新加载页面
            $_SESSION['success'] = '登录成功';
            header('Location: ./index.php');
            exit;
 
 } else {
       // 登录失败记录在session中
       $_SESSION['error'] = '非常抱歉,您输入的用户名或密码有误';
 }
}
?>

Copy code

The principle is very simple. The user submits the user name and password, and then we go through security processing, and then go to mysql to accurately compare them. If they are the same, the login is successful. http://www.bkjia.com/PHPjc/628714.htmlwww.bkjia.comtrue
http: //www.bkjia.com/PHPjc/628714.html
TechArticle
In php, when we want to log in, we generally do it by combining mysql session. I will give you one below. Examples to introduce the php+mysql+session login example program code. If you need to learn...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn