doupingpeng7567 2016-07-09 23:26
浏览 28
已采纳

如果用户在线或离线,我该如何回应?

I found a code that transform a timestamp from when a user last logged in. It really short but works just fine displaying x seconds, 2 days, etc.

The problem is that i wish to transform the first x minutes (max 5min) to say <div class="green">ONLINE</div>

heres the php:

<?php
function humanTiming ($time)
        {

            $time = time() - $time; // to get the time since that moment
            $time = ($time<1)? 1 : $time;
            $tokens = array (
                31536000 => 'year',
                2592000 => 'month',
                604800 => 'week',
                86400 => 'day',
                3600 => 'hour',
                60 => 'minute',
                1 => 'second'

                // Tried to replace seconds with <div class="green">ONLINE</div>
                // but will end up looking like x ONLINEs where x = seconds
            );

            foreach ($tokens as $unit => $text) {
                if ($time < $unit) continue;
                $numberOfUnits = floor($time / $unit);
                return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'');
            }

        }

echo humanTiming( strtotime($user_last_life) );
?>

Followup question : What is the most secure and best way to update a database with timestamp? I have problems having it update after login but have added this code in my head.php

$updated_life_id = htmlentities($_SESSION['user']['id'], ENT_QUOTES, 'UTF-8');
$set_last_life = "UPDATE users SET last_life = time() WHERE id = '$updated_life_id'";
$datab->query($set_last_life);
  • 写回答

1条回答 默认 最新

  • dongwen1909 2016-07-09 23:32
    关注
    if(strtotime($user_last_life) >= time()-300)   // 300 seconds = 5 minutes
    {
      echo '<div class="green">ONLINE</div>';
    }
    

    And for your database question

    How can I prevent SQL injection in PHP?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 手机连接电脑热点显示无ip分配
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大