session效验

看代码。。。

<?php
    $servername = "服务器名";
    $username = "账户名";
    $password = "密码";
    $dbname = "数据库名";
?>
<?php
    // Session需要先启动。
    session_start();
    //判断uname和pwd是否赋值
    if(isset($_POST['uname']) && isset($_POST['pwd'])){
        $name = $_POST['uname'];
        $pwd = $_POST['pwd'];
        //连接数据库
        $conn = new mysqli($servername, $username, $password, $dbname);
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }
        //验证内容是否与数据库的记录吻合。
        $sql = "SELECT * FROM test_students_all WHERE (student_name='$name') AND (password='$pwd')";
        //执行上面的sql语句并将结果集赋给result。
        $result = $conn->query($sql);
        //判断结果集的记录数是否大于0
        if ($result->num_rows > 0) {
            $_SESSION['user_account'] = $name;
            // 输出每行数据
            while($row = $result->fetch_assoc()) {
                echo '<p>' . $row['student_nbr'] . '<br/>' . $row['student_name'] .  '(' . $row['sex'] . ')' . '<br/>' . $row['class'] . '<br/>' . $row['major'].'</p>';
                // <p><img src="student_images/CLASS/STUDENT_NBR.jpg" /></p>
                echo '<p><img src="student_images/' . $row['class'] . '/' . $row['student_nbr'] . '.jpg" /></p>';
            }
        } else {
            echo "没有您要的信息";
        }
        $conn->close();       
    }
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录校验</title>
</head>
<body>
    <p>
        <?php
            // isset(xx) 测试xx是否设置了
            if(isset($_SESSION['user_account'])){
                echo '你好,' . $_SESSION['user_account'];
            }
            else{
                echo '游客';
            }
            //$conn->close();
        ?>
    </p>
    <form method="POST">
        <input type="text" name="uname" placeholder="用户名" />
        <br />
        <input type="password" name="pwd" placeholder="密码" />
        <br />
        <input type="submit">
    </form>
</body>
</html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65

看看效果。。。 
这里写图片描述

posted @ 2017-11-27 11:33  py小蟒蛇  阅读(261)  评论(0编辑  收藏  举报