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

时间:2016-07-09 23:26:24

标签: php

我找到了一个代码,用于转换用户上次登录时的时间戳。它非常简短但可以正常显示x秒,2天等。

问题是我希望将前x分钟(最长5分钟)改为<div class="green">ONLINE</div>

继承人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) );
?>

后续问题: 使用时间戳更新数据库的最安全和最佳方法是什么? 我在登录后更新它有问题但是在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 个答案:

答案 0 :(得分:1)

li.button {
    background-color: transparent;
    padding: 2px 6px 2px 6px;
    border-radius: 15px;
    border-top: 3px solid #F2F2F2;
    border-right: 2px solid #260000;
    border-bottom: 2px solid #450000;
    border-left: 3px solid #D1D1D1;
    margin-bottom: 2px;
}

对于您的数据库问题

How can I prevent SQL injection in PHP?