简体   繁体   English

从Javascript运行PHP文件以检查在线用户:表示未登录的用户是

[英]Run PHP files from Javascript to check Online users: says users who are not logged in are

I am writing just a simple admin panel but want to enable users who are logged in to see other logged in users. 我仅在编写一个简单的管理面板,但想使已登录的用户能够查看其他已登录的用户。 To do this, I have a sessions MySQL table which contains the active sessions (logged in users) and a PHP file which essentially will update the timestamp of each active user every few seconds using JavaScript on all pages (so it will only update if active and will delete sessions that are greater than 30 seconds ago). 为此,我有一个会话MySQL表,其中包含活动会话(已登录用户)和一个PHP文件,该文件实际上将在所有页面上使用JavaScript每隔几秒钟更新每个活动用户的时间戳(因此,只有在活动状态下才会更新)并删除大于30秒的会话)。 Here is the code for that: 这是该代码:

Javascript: Javascript:

<script type="text/javascript">
function track() {
var url = "tracking.php?username=<?PHP echo $_SESSION['username']; ?>";
xmlReq=new XMLHttpRequest();
xmlReq.open("GET",url,true);
xmlReq.send();
}
setInterval(function(){track();}, 3000);
</script>

PHP: So the url would be tracking.php?username=john.doe PHP:因此该网址将为tracking.php?username = john.doe

<?PHP
$username=$_GET['username'];
if($username){
$result = mysql_query("SELECT * FROM sessions WHERE username='$username'");

if(mysql_fetch_array($result) !== false){
mysql_query("UPDATE sessions SET time=now() WHERE username='$username'");
} else {
mysql_query("INSERT INTO `sessions` (`username`, `time`) VALUES ('$username',now())") or die (mysql_error());
}
mysql_query("DELETE FROM sessions WHERE time < (now() - 30);");
}
?>

This works fine and the session table updates every 3 seconds. 这可以正常工作,并且会话表每3秒更新一次。 The problem is when I come to display which users are currently logged in. I have a check.php file which contains the following: 问题是当我显示当前登录的用户时。我有一个check.php文件,其中包含以下内容:

<?PHP
$username=$_GET['username'];
$result=mysql_query("SELECT * FROM sessions WHERE username = '$username'");
$num=mysql_num_rows($result);
if($num>0){
echo "Online";
} else {
echo "Offline";
}
?>

And the Javascript function: 和Javascript函数:

<script type="text/javascript">
function checklogin(username) {
var element = "status";
newusername = username.substring(0, username.indexOf('.'));
newelement = element + newusername;
$.ajax({
url: ‘check.php?username='+username,
type: "GET",
dataType: "html",
success: function(data)
{
if(data === "Online")
{
document.getElementById(newelement).innerHTML='<div class="online"></div>';
}
}
});
}
</script>

Which would change the div of the user: 这将更改用户的div:

<?PHP
$sql=mysql_query("SELECT * FROM tbl_admin_users order by id ASC");
$num=mysql_num_rows($sql);
$i=0;
while($num>$i){
$id=mysql_result($sql,$i,"id");
$username=mysql_result($sql,$i,"username");
$first_name=mysql_result($sql,$i,"firstname");
$last_name=mysql_result($sql,$i,"lastname");
$jobtitle=mysql_result($sql,$i,"jobtitle");
$email=mysql_result($sql,$i,"email");
$phone=mysql_result($sql,$i,"phone_office");
$spanar=explode(".",$username);
$spanid=$spanar[0];
?>

<tr>
<td class="center" width="40px">
<script type="text/javascript">
setInterval(function(){checklogin('<?PHP echo $username; ?>');}, <?PHP echo rand(2000,4000); ?>);
</script>
<span id="status<?PHP echo $spanid?>"></span>
</td>

Now everything seems to be working fine apart from over time other users who are not active and are not in the session table are being displayed as online. 现在,随着时间的推移,一切似乎都运行良好,但随着时间的推移,其他处于非活动状态且不在会话表中的用户将显示为联机。 For the life of me I am unable to see where the problem is. 对于我的一生,我无法看到问题出在哪里。 Is it with the javascript or the PHP checking the DB? 是使用JavaScript还是PHP检查数据库?

Could the file be running on the server in the background once run and it picks up the previous running of the check.php which was online and moving it onto the other user? 文件一旦运行就可以在后台服务器上运行,并且可以获取在线的check.php的先前运行并将其移至其他用户吗? Do I need to close the file after retrieving the online/offline? 检索在线/离线后是否需要关闭文件? I have tried to ensure that the time the file is run is randomised so as not to run at the same time. 我试图确保文件的运行时间是随机的,以便不要同时运行。

Any help would be appreciated. 任何帮助,将不胜感激。

Seems like your Javascripts are getting cached responses. 似乎您的Javascript正在获得缓存的响应。 disable caching on the PHP files. 禁用对PHP文件的缓存。

Use headers in you php: 在您的php中使用标头:

<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM