php用户登陆 验证用户名密码和注销用户信息

登陆页面login.php

<!DOCTYPE html>
<html>
<head>
<title>登录</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
    <div class="content" align="center">
        <!--头部-->
        <div class="header">
        <h1>登录</h1>
        </div>
        <!--中部-->
        <div class="middle">
            <form id="loginform" action="loginaction.php" method="post">
                <table border="0">
                    <tr>
                        <td>用户名:</td>
                        <td>
                            <input type="text" id="name" name="username" required
                            value="<?php echo isset($_COOKIE["wtcl_shyh"])?$_COOKIE["wtcl_shyh"]:"";?>">
                        </td>
                    </tr>
                    <tr>
                        <td>密&nbsp;码:</td>
                        <td><input type="password" id="password" name="password"></td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <input name="remember" type="checkbox"><small>记住我
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2" align="center" style="color:red;font-size:14px;">
                        <!--提示信息-->
                         <?php
                                $err=isset($_GET["err"])?$_GET["err"]:"";
                                switch($err) {
                                    case 1:
                                    echo "用户名或密码错误!";
                                    break;
                                    case 2:
                                    echo "用户名或密码不能为空!";
                                    break;
                                }
                            ?>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2" align="center">
                            <input type="submit" id="login" name="login" value="登录">
                            <input type="reset" id="reset" name="reset" value="重置">
                        </td>
                    </tr>
                    <tr>

                    </tr>
                </table>
            </form>
        </div>
        <!--脚部-->
        <div class="footer">
        </div>
    </div>

</body>
</html>

验证用户名和密码loginaction.php

<?php $username = isset($_POST['username'])?$_POST['username']:"";//声明变量
    $password = isset($_POST['password'])?md5($_POST['password']):"";
    $remember = isset($_POST['remember'])?$_POST['remember']:"";
    //判断用户名和密码是否为空
    if(!empty($username)&&!empty($password)) {
    //准备SQL语句
        require_once('comm.php');
        mysql_select_db($database_wtcl_comm); //打开数据库
        mysql_query("set names 'utf8'"); 
        $sql_select = "SELECT * FROM wtcl_yh WHERE yh_ming='$username' AND yh_mi='$password'";
        //执行SQL语句
        $ret = mysql_query($sql_select) or die(mysql_error()); ;
        $row = mysql_fetch_array($ret);
          //判断用户名或密码是否正确
        if($username==$row['yh_ming']&&$password==$row['yh_mi']) {
            //选中“记住我”
            if($remember=="on") {
                //创建cookie
                setcookie("wtcl_shyh", $username, time()+7*24*3600);
            }
            //开启session session_unset()
            @session_start();
            //创建session
            $_SESSION['wtcl_user'] = $username;
            if(!empty($_SESSION['wtcl_user'])){
                date_default_timezone_set('PRC');
                $zuijin = date('Y-m-d H:i:s');
                mysql_select_db($database_wtcl_comm); 
               $query=mysql_query("update wtcl_yh set yh_zuijin='$zuijin' where yh_ming='$username'");//更新最近登陆时间
             //跳转到loginsucc.php页面
            /* echo '<SCRIPT LANGUAGE="javascript">location.href="0.php"</SCRIPT>'; */
          header('Location:index.php');
          }
            //写入日志
            /* $ip = $_SERVER['REMOTE_ADDR'];
            $date = date('Y-m-d H:m:s');

            $info = sprintf("当前访问用户:%s,IP地址:%s,时间:%s \n",$username, $ip, $date);
            $sql_logs = "INSERT INTO Logs(username,ip,date) VALUES('$username','$ip','$date')";

            //日志写入文件,如实现此功能,需要创建文件目录logs
            $f = fopen('./logs/'.date('Ymd').'.log','a+');

            fwrite($f,$info);
            fclose($f); */
            //关闭数据库
            mysql_close();
        }else {
            //用户名或密码错误,赋值err为1
            echo '<SCRIPT LANGUAGE="javascript">location.href="login.php?err=1"</SCRIPT>';
            //header("Location:login.php?err=1");
 }
    }else {
        //用户名或密码为空,赋值err为2
        echo '<SCRIPT LANGUAGE="javascript">location.href="login.php?err=2"</SCRIPT>';
        //header("Location:login.php?err=2");
}
?>

注销用户信息logout.php

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>注销</title>
</head>
<body>
<?php 
@session_start('wtcl_user');
@session_destroy('wtcl_user');
echo '<SCRIPT LANGUAGE="javascript">location.href="login_err.php"</SCRIPT>';
?>
</body>
</html>

主页根据用户登陆情况显示信息index.php

<?php
set_time_limit(0);
@session_start();
$username=isset($_SESSION['wtcl_user'])?$_SESSION['wtcl_user']:"";//判断session是否为空
if(!empty($username)){
    $yhming=$_SESSION['wtcl_user'];
    mysql_query("set names 'utf8'"); //数据库输出编码 应该与你的数据库编码保持一致,建议用UTF-8 国际标准编码.
mysql_select_db($database_wtcl_comm); //打开数据库
$sql="select * from wtcl_yh where yh_ming='$yhming'";//从数据库中取出用户信息
$rs_yh = mysql_query($sql) or die(mysql_error()); 
$yonghu = mysql_fetch_assoc($rs_yh); 
$_SESSION['wtcl_user']=$yonghu['yh_ming'];}
else{echo '<SCRIPT LANGUAGE="javascript">location.href="login_err.php"</SCRIPT>';}
?>
<?php $yhming=$_SESSION['wtcl_user'];
    if($yhming==$yonghu['yh_ming']&&$yonghu['yh_add'] == 1){echo '欢迎你:'.$yonghu['yh_mingzi'].',<a href="logout.php">退出</a>|<a href=../wtcl/admin/index.html target="_blank">管理</a>';}
        elseif($yhming==$yonghu['yh_ming']&&$yonghu['yh_add'] != 1){echo '欢迎你:'.$yonghu['yh_mingzi'].',<a href="logout.php">退出</a>';}
        elseif(!$yhming){echo '<SCRIPT LANGUAGE="javascript">location.href="login_err.php"</SCRIPT>';} ?>

登陆错误页面login_err.php

<?php require_once('comm.php'); ?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>请登陆</title>
<link rel="shortcut icon" href="../wtcl/tb.ico" />
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table width="960" border="0" align="center" cellpadding="0" cellspacing="0">
    <tr>
      <td height="60" align="center" valign="top" class="biaoti">
       <p>用户名或密码有误</p>
       <p>或登陆超时</p>
      <p>请重新<a href="login.php" style="font-size: 20px"> 登陆</a></p>

      <p>&nbsp;</p></td>
    </tr>
  </table>
</body>
</html>
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 PHP + MySQL 用户注册登录验证的示例代码: 注册页面(register.php) ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>用户注册</title> </head> <body> <h2>用户注册</h2> <form action="register.php" method="post"> <label for="username">用户名:</label> <input type="text" name="username" id="username"><br> <label for="password">密码:</label> <input type="password" name="password" id="password"><br> <label for="confirm_password">确认密码:</label> <input type="password" name="confirm_password" id="confirm_password"><br> <input type="submit" value="注册"> </form> </body> </html> ``` register.php 处理用户提交的注册信息 ```php <?php // 连接 MySQL 数据库 $mysqli = new mysqli('localhost', 'root', 'password', 'test'); if ($mysqli->connect_errno) { echo '连接 MySQL 失败:' . $mysqli->connect_error; exit(); } // 处理用户提交的注册信息 if ($_SERVER['REQUEST_METHOD'] === 'POST') { // 获取表单提交的数据 $username = $_POST['username']; $password = $_POST['password']; $confirm_password = $_POST['confirm_password']; // 验证密码是否相等 if ($password !== $confirm_password) { echo '两次输入的密码不相等'; exit(); } // 查询用户名是否已经存在 $sql = "SELECT * FROM users WHERE username='$username'"; $result = $mysqli->query($sql); if ($result->num_rows > 0) { echo '用户名已经存在'; exit(); } // 插入新用户数据到数据库 $hashed_password = password_hash($password, PASSWORD_DEFAULT); $sql = "INSERT INTO users (username, password) VALUES ('$username', '$hashed_password')"; if ($mysqli->query($sql)) { echo '注册成功'; } else { echo '注册失败:' . $mysqli->error; } } // 关闭数据库连接 $mysqli->close(); ``` 登录页面(login.php) ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>用户登录</title> </head> <body> <h2>用户登录</h2> <form action="login.php" method="post"> <label for="username">用户名:</label> <input type="text" name="username" id="username"><br> <label for="password">密码:</label> <input type="password" name="password" id="password"><br> <input type="submit" value="登录"> </form> </body> </html> ``` login.php 处理用户提交的登录信息 ```php <?php // 启动会话 session_start(); // 连接 MySQL 数据库 $mysqli = new mysqli('localhost', 'root', 'password', 'test'); if ($mysqli->connect_errno) { echo '连接 MySQL 失败:' . $mysqli->connect_error; exit(); } // 处理用户提交的登录信息 if ($_SERVER['REQUEST_METHOD'] === 'POST') { // 获取表单提交的数据 $username = $_POST['username']; $password = $_POST['password']; // 查询用户数据 $sql = "SELECT * FROM users WHERE username='$username'"; $result = $mysqli->query($sql); if ($result->num_rows > 0) { $user = $result->fetch_assoc(); // 验证密码是否正确 if (password_verify($password, $user['password'])) { // 登录成功,保存用户信息到会话 $_SESSION['user_id'] = $user['id']; $_SESSION['username'] = $user['username']; echo '登录成功'; } else { echo '密码错误'; } } else { echo '用户名不存在'; } } // 关闭数据库连接 $mysqli->close(); ``` 注销页面(logout.php) ```php <?php // 启动会话 session_start(); // 删除保存的用户信息 unset($_SESSION['user_id']); unset($_SESSION['username']); echo '注销成功'; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值