<?php
//case 20 用户登录
/**
 *
 *
 *
 *
 *
 */
?>
<html>
<head>
    <title>用户登录</title>
</head>
<body>
<form method="post" action="./t209.php">
    <sapn>
        用户名:<input type="text" name="user_name" id="user_name">
        密码:<input type="password" name="pwd" id="pwd">
        <input type="submit" name="login" value="登录">
        <input type="reset" value="重新填写">
    </sapn>
</form>
</body>
</html>

<?php
//登录
if(isset($_POST['login']))
{
    //用户名
    $user_name = isset($_POST['user_name'])?trim($_POST['user_name']):'';
    //密码
    $pwd = isset($_POST['pwd'])?$_POST['pwd']:'';
    //连接数据库
    $connect = mysqli_connect('localhost','root','mysql123','test');
    if(!$connect)
    {
        die('数据库连接失败!'.mysqli_error($connect));
    }
    //设置字符集
    mysqli_query($connect,'set names utf8');
    //防sql注入
    $user_name = mysqli_real_escape_string($connect,$user_name);
    $sql = "select * from emp_user where user_name='$user_name'";
    if($rst = mysqli_query($connect,$sql))
    {
        $row = mysqli_fetch_assoc($rst);
        //密码加密
        $pwd = md5($pwd);
        //密码相等
        if($pwd == $row['password'])
        {
            echo "$user_name,登录成功";
            //启动会话层
            session_start();
            //保存会话
            $_SESSION['user_info'] = array(
                'id'=>$row['id'],
                'user_name'=>$user_name
            );
            echo "<a href='./t209.php?action=e1'>注销退出</a>";
        }
        else
        {
            die('登录失败');
        }
    }


}
//注销
if(isset($_GET['action']))
{
    session_start();
    if($_GET['action'] == 'e1')
    {
        //清空会话
        unset($_SESSION['user_info']);
    }
    header('Location:./t209.php');
}
?>