我希望在刷新网站时从数据库中获取更新信息,而不必退出并重新登录。
我在登录时获取user.php的信息,然后将它们重定向到memberpage.php。当他们刷新memberpage.php时,如果数据库上有任何更改,它将在memberpage上更改。因此,如果他们使用isBanned_reason = 0登录,然后我在数据库上将其更改为1,我希望在成员页面上更改它。
继承我的代码: user.php的
<?php
include('password.php');
class User extends Password{
private $_db;
function __construct($db){
parent::__construct();
$this->_db = $db;
}
private function get_user_hash($username){
try {
$stmt = $this->_db->prepare('SELECT password, username, memberID, isBanned_reason, Rank FROM members WHERE username = :username AND active="Yes" ');
$stmt->execute(array('username' => $username));
return $stmt->fetch();
} catch(PDOException $e) {
echo '<p class="bg-danger">'.$e->getMessage().'</p>';
}
}
public function login($username,$password){
$row = $this->get_user_hash($username);
if($this->password_verify($password,$row['password']) == 1){
$_SESSION['loggedin'] = true;
$_SESSION['username'] = $row['username'];
$_SESSION['memberID'] = $row['memberID'];
$_SESSION['Rank'] = $row['Rank'];
$_SESSION['isBanned_reason'] = $row['isBanned_reason'];
return true;
}
}
public function logout(){
session_destroy();
}
public function is_logged_in(){
if(isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true){
$_SESSION['Rank'];
$_SESSION['isBanned_reason'];
return true;
}
}
}
?>
memberpage.php
<?php require('includes/config.php');
//if not logged in redirect to login page
if(!$user->is_logged_in()){ header('Location: login.php'); }
//define page title
$title = 'Members Page';
//include header template
?>
<link rel="stylesheet" type="text/css" href="style/Stylememberpage.css">
<?php if ($_SESSION['isBanned_reason'] == 0) {?>
<body>
<center>
<p>
<h1 id="Caption1">Secret website title</h1>
<h2 id="Caption2">You are logged in as a staff member</h2>
<ul id="menu">
<li id="items"><a href="index.php">Home</a></li>
<li id="items"><a href="registrer.php">Staff Site</a></li>
<li id="items"><a href="Leaderboards.html">Games</a></l1>
<li id="items"><a href="Contact.html">Leaderboard</a></li>
<li id="items"><a href="logout.php">Log out</a></li>
</body>
</ul>
</p>
</center>
<?php
}
?>
<?php if ($_SESSION['isBanned_reason'] == 1) {?>
<body>
<center>
<p>
<h1 id="Banned1">Your account has been locked.</h1>
<h2 id="Banned2">You are banned for until: 12.12.2020 reason : scamming/exploit.</h2>
<ul id="menu">
<li id="items"><a href="index.php">Home</a></li>
<li id="items"><a href="registrer.php">Staff Site</a></li>
<li id="items"><a href="Leaderboards.html">Games</a></l1>
<li id="items"><a href="Contact.html">Leaderboard</a></li>
<li id="items"><a href="logout.php">Log out</a></li>
</body>
</ul>
</p>
</center>
<?php
}
?>
而且,如果可能的话。当我在我的数据库上更改isBanned_reason时,是否有任何方法可以&#34;应用&#34;没有用户刷新页面?因为我希望它们在改变后立即被禁止。我怎么能这样做?
答案 0 :(得分:1)
您可以使用jQuery Ajax
函数检查服务器或数据库是否有更改的值,还可以使用setInterval
函数在每个5 seconds
上触发ajax
试试这样:
Jquery Ajax:(在您希望连续触发检查的网页上)
setInterval(function(){
$.ajax({
url : 'check_db.php', //URL: lets say you have check_db.php in same directory where this file is
dataType : 'html',
data : 'post',
data : {user_id : 'user_id'},
success : function(response){
//catch the response here from server
if (response == 1) {
window.location.href="logout.php"; //redirecting user to logout
}
}
})
},5000);//Every 5 seconds
此外,请务必加载jquery
库以在页面顶部运行jquery
这样的功能
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
如果您不想将页面重定向到登录并要求重新登录,那么在此AJAX调用的成功方法中,您需要编写另一个AJAX来设置会话变量并隐藏禁止区域。更新会话变量是必需的,因为如果用户刷新页面,则if语句必须正常工作以检查禁止的会话变量。
这就是为什么清楚地学习AJAX会更好的原因。 Link
答案 1 :(得分:0)
您不必使用jquery。这是原生的javascript也可以运行:
var objXhttp = new XMLHttpRequest();
objXhttp.open("POST", "http://YourServer.com/CheckForBadUser.php, false);
objXhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
objXhttp.send("nUserID=" + gnUserID);
if (objXhttp.response == 1) {
ThrowUserOut()
}
执行以下步骤以使此代码有效:
创建CheckForBadUser.php,检查UserID是否已标记为错误。
在javascript中创建一个名为gnUserID的全局变量。
创建一个名为ThrowUserOut()的javascript函数,如果CheckForBadUser.php返回1,则执行该函数。
function ThrowUserOut () {
document.all.idDivMain.innerHTML = "You've been thrown out!"
}
在body标签的开头添加一个名为idDivMain的div。记得在结束身体之前关闭那个div。