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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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
}
?>