我知道我可能会以最糟糕的方式做到这一点,而且最重要的是它现在还没有工作。
所以我要做的是在每个用户上线时显示通知。我有一些阿贾克斯:
<!-- display online status notification Script -->
<script>
$(document).ready(function(){
$.ajax({
url: 'check-online-status.php',
cache: false,
error: function(e){
alert(e);
},
success: function(response){
// Reload the page to refresh data from database
window.setTimeout(function(){ document.location.reload(true); }, 10000);
}
});
});
</script>
将检查check-online-status.php文件中的哪些用户在线:
<?php
//// require the db config file ////
require("../config.php");
//// Get users ////
$query5 = "SELECT online_status, last_activity FROM users ";
$stmt5 = $db->prepare($query5);
$stmt5->execute();
$recent_user = $stmt5->fetchAll();
?>
<?php foreach($recent_user as $row) { ?>
<?php
// Check online status
if($row['online_status'] == 'Online'){
$online_status = 'online';
}
// Get last activity date/time and compare to todays date/time
if(!is_null($row['last_activity'])){
$start_date = new DateTime($row['last_activity']);
$end_date = new DateTime(date("Y-m-d h:i:sa"));
$interval = $start_date->diff($end_date);
$last_activity = "Last Active: " . $interval->y . " years, " . $interval->m." months, ".$interval->d." days, ".$interval->h." hours, ".$interval->i." minutes "."and ".$interval->s." seconds "."ago";
if($interval->s == 0){
$last_activity = "Last Active: Now";
}
}
else {
$last_activity = 'None';
}
?>
<?php if($online_status == 'online' && $last_activity == 'Last Active: Now' ){ ?>
<!-- Launch a SmallBox on form load -->
<script type="text/javascript">
$(document).ready(function(){
var username = <?php echo ''.$row['username'].''.'is online'; ?>;
$.bigBox({
title: "Online",
content: username,
width: 250,
color: "#2ECC71",
timeout: 5000,
delay: 0.5,
});
});
</script>
<?php } ?>
我正在使用名为Metro Notification的脚本来显示通知框。
不确定我做错了什么。当我用firebug检查时,我可以通过JS(位于check-online-status.php文件中)看到信息看起来正确:
<!-- Launch a SmallBox on form load -->
<script type="text/javascript">
$(document).ready(function(){
var username = user1 is online;
$.bigBox({
title: "Online",
content: username,
width: 250,
color: "#2ECC71",
timeout: 5000,
delay: 0.5,
});
});
</script>
<!-- Launch a SmallBox on form load -->
<script type="text/javascript">
$(document).ready(function(){
var username = user2 is online;
$.bigBox({
title: "Online",
content: username,
width: 250,
color: "#2ECC71",
timeout: 5000,
delay: 0.5,
});
});
</script>
如果2个用户在线,这是我上面的代码。