PHP小例子(1)--jQuery检查session过期后并实现跳转

PHP7.2+Bootstrap 5测试,未通过,貌似jsquery中的setinterval有问题。
用到的数据库文件

  --  
 -- Table structure for table `tbl_users`  
 --  
 CREATE TABLE IF NOT EXISTS `tbl_users` (  
  `id` int(11) NOT NULL AUTO_INCREMENT,  
  `username` varchar(20) NOT NULL,  
  `password` varchar(20) NOT NULL,  
  PRIMARY KEY (`id`)  
 ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;  

index3.php

<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>

    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">

    <title>Hello, world!</title>
        <style>  
            #box  
            {  
                width:600px;  
                background:gray;  
                color:white;  
                margin:0 auto;  
                padding:10px;  
                text-align:center;  
            }  
        </style>
        <script>  
            $(document).ready(function(){  
                function check_session()
                {
                    $.ajax({
                        url:"check_session.php",
                        method:"POST",
                        success:function(data)
                        {
                            if(data == '1')
                            {
                                alert('登陆时间过长,请重新登陆');  
                                window.location.href="login.php";
                            }
                        }
                    })
                }
                setInterval(function(){
                    check_session();
                }, 1000);  //10000 means 10 seconds
            });  
        </script>  
  </head>
  <body>
    <?php  
        session_start();  
        if(isset($_SESSION["name"]))  
        {  
            echo "<h1 align='center'>使用Ajax查询session是否过期</h1>";
            echo "<h1 align='center'>".$_SESSION["name"]."</h1>";  
            echo "<p align='center'><a href='logout.php'>Logout</a></p>";  
        }  
        else  
        {  
            header('location:login.php');  
        }  
        ?>  
   

    <!-- Optional JavaScript; choose one of the two! -->

    <!-- Option 1: Bootstrap Bundle with Popper -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.bundle.min.js" integrity="sha384-ygbV9kiqUc6oa4msXn9868pTtWMgiQaeYH7/t7LECLbyPA2x65Kgf80OJFdroafW" crossorigin="anonymous"></script>

    
  </body>
</html>

login.php

<?php  
    //login.php  
    $connect = mysqli_connect("localhost", "root", "password", "test");
    session_start();  
    if(isset($_POST["sub"]))  
    {  
        $username = mysqli_real_escape_string($connect, $_POST["name"]);
        $password = mysqli_real_escape_string($connect, $_POST["pass"]);
        $query = "SELECT * FROM tbl_users WHERE username = '".$username."' AND password = '".$password."'";
        $result = mysqli_query($connect, $query);
        if(mysqli_num_rows($result) > 0)
        {
            $_SESSION["name"] = $_POST["name"];
            header("location:index3.php"); 
        }
        else
        {
            echo '<script>alert("用户名与密码不配")</script>';
        }    
    }  
 ?>  
<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">

    <title>Hello, world!</title>
        <style>  
            #box  
            {  
                    width:600px;  
                    background:gray;  
                    color:white;  
                    margin:0 auto;  
                    padding:10px;  
                    text-align:center;  
                    margin-top:100px;
            }  
        </style>  
    </head>
    <body>
        <div id="box">
            <h1 class="text-center">Ajax检查session是否过期,并跳回自动登陆界面</h1>  
            <h2>登陆界面</h2>  
            <form method="post">  
                <input type="text" name="name" id="name" placeholder="输入用户名" class="form-control" /><br />  
                <input type="password" name="pass" id="pass" placeholder="输入密码" class="form-control" /><br />  
                <input type="submit" name="sub" id="sub" class="btn btn-info" value="登陆" />  
            </form>  
            <br /><br />  
        </div>  
    

        <!-- Optional JavaScript; choose one of the two! -->

        <!-- Option 1: Bootstrap Bundle with Popper -->
        <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.bundle.min.js" integrity="sha384-ygbV9kiqUc6oa4msXn9868pTtWMgiQaeYH7/t7LECLbyPA2x65Kgf80OJFdroafW" crossorigin="anonymous"></script>

        
    </body>
</html>

logout.php

<?php  
    //logout.php  
    session_start();  
    session_destroy();  
    header('location:login.php');  
 ?>

check_session.php

<?php  
    //check_session.php  
    session_start();
    if(isset($_SESSION["name"]))  
    {  
        echo '0';     //session not expired       
    }  
    else  
    {  
        echo '1';     //session expired  
    }
 ?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值