<legend id="ra9il"><dl id="ra9il"></dl></legend>

  • <legend id="ra9il"><dl id="ra9il"></dl></legend>
  • <legend id="ra9il"><dl id="ra9il"></dl></legend>

    <optgroup id="ra9il"></optgroup>

    <span id="ra9il"><output id="ra9il"></output></span>
    <ol id="ra9il"></ol>
    <ol id="ra9il"></ol>

  • 作业总结:

    session和cookie基本一样,不同的是session是保存到服务器上的,cookie是保存到客户端的。

    每个页面要在顶部加载session_start();否则无法记录数据。

    cookie获取值的方式和seesion一样,但是设置的地方有区别:

    $_SESSION['username'] = $user['username'];

    setcookie('username',$user['username']);


    1. 登录页面 admin.php  

    <?php
    session_start();
    if(isset($_SEESION['username']) || $_SEESION['username'] === 'admin'){
        echo '<script>
     alert("请不要重复登录");
     location.assign("admin.php");
              </script>';
    }
    ?>
    <!doctype html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport"
     content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>LOGIN</title>
    </head>
    <body>
        <p>登录首页</p>
        <form action="ckeck.php" method="post" onsubmit="return noEmpty()">
            <label for="">邮箱:</label>
            <input type="text" name="email" id="email">
            <label for="">密码:</label>
            <input type="password" name="password" id="password">
            <button>登录</button>
        </form>
        <script>
            function noEmpty(){
                let email = document.getElementById('email').value;
                let password = document.getElementById('password').value;
                if(password.length==0 || email.length==0){
                    alert("请输入邮箱和密码");
                    return false;
                }
            }
        </script>
    </body>
    </html>



    2. 登录验证页面 ckeck.php

    $_SESSION['username'] = $user['username'];设置和cookie不一样。

    <?php
        session_start();
        if(isset($_SEESION['username'])|| $_SEESION['username'] ==='admin'){
            echo '<script>
                 alert("请不要重复登录");
                 location.assign("admin.php");
                 </script>';
        }
        require __DIR__ ."/include/connect.php";
    
    
    
        $sql = 'SELECT * FROM  `user` WHERE `email` = :email AND `password` = :password LIMIT 1';
    
    
        $email = $_POST['email'];
    
        $password = $_POST['password'];
    
        $stmt = $pdo->prepare($sql);
    
        $stmt->execute(['email'=>$email,'password'=>$password]);
    
        $user = $stmt->fetch(PDO::FETCH_ASSOC);
    
        if($user===false){
            echo '<script>
                 alert("请登录");
                 location.assign("login.php");
                 </script>';
        }
        $_SESSION['username'] = $user['username'];
            echo '<script>
                 alert("登录成功");
                 location.assign("admin.php");
                 </script>';
    
    
    ?>

    3.    后台首页 admin.php

    <?php
    session_start();
    
        if(isset($_SESSION['username'])|| $_SESSION['username'] ==='admin')  : ?>
     
    <!doctype html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport"
     content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>后台管理</title>
    </head>
    <body>
    <p>欢迎<?php echo $_SESSION['username']; ?>登录</p>
    <p><a href="out.php">退出登录</a></p>
        <?php else  : ?>
     <script>alert("请登录");location.assign('login.php');</script>
    </body>
    </html>
    
            <?php  endif; ?>

    4. 注销登录页面 out.php

    cookie的注销方式和seesion不一样。

    session_destroy();是直接删除服务器上的文件

    <?php
    session_start();
    if(isset($_SESSION['username'])|| $_SESSION['username'] ==='admin'){
        session_destroy();
        echo '<script>
               alert("返回登录页面");
               location.assign("login.php");
               </script>';
    }else{
        echo '<script>
               alert("请登录");
               location.assign("login.php");
               </script>';
    }
    
    
    
        ?>


    0/300
    全部评价
    今开奖结果-今期开奖结果现场-今期开奖结果直