Home >php教程 >PHP源码 >php登录函数login session+mysql

php登录函数login session+mysql

PHP中文网
PHP中文网Original
2016-05-25 17:08:101146browse

[PHP]代码  

<?php
// 为php和mysql剔除不安全html代码。
//http://www.php.cn/
function safestrip($string){
   $string = strip_tags($string);
   $string = mysql_real_escape_string($string);
   return $string;
}
 
//登录信息显示函数
function messages() {
 $message = &#39;&#39;;
 if($_SESSION[&#39;success&#39;] != &#39;&#39;) {
   $message = &#39;<span id="message">&#39;
   .$_SESSION[&#39;success&#39;].&#39;</span>&#39;;
   $_SESSION[&#39;success&#39;] = &#39;&#39;;
 }
 if($_SESSION[&#39;error&#39;] != &#39;&#39;) {
   $message = &#39;<span id="message">&#39;
   .$_SESSION[&#39;error&#39;].&#39;</span>&#39;;
   $_SESSION[&#39;error&#39;] = &#39;&#39;;
 }
 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 = &#39;$user&#39;
 AND password = &#39;$pass&#39;")or die(mysql_error());
 
 //如果=1则表示认证成功
 if (mysql_num_rows($sql) == 1) {
 
             //开始记录在session中
             $_SESSION[&#39;authorized&#39;] = true;
 
             // 重新加载页面
            $_SESSION[&#39;success&#39;] = &#39;登录成功&#39;;
            header(&#39;Location: ./index.php&#39;);
            exit;
 
 } else {
       // 登录失败记录在session中
       $_SESSION[&#39;error&#39;] = &#39;非常抱歉,您输入的用户名或密码有误&#39;;
 }
}
?>

                   

                   

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