Welcome Guest, Not a member yet? Register   Sign In
Is my online function OK or does it need improvment?

(This post was last modified: 02-25-2017, 12:59 AM by wolfgang1983.)

I would like to know if my code is suitable for checking if user is online seems to work.

When user not online it gets the users time stamp from database by accessing id on profile url.


Does the online function below need any improvement suggestions please thank you.

[Image: 3DaSi4jx1eND.png]

PHP Code:
public function online($timestamp){
        $offline =  time() - 50;

    if ($this->auth->islogged()) {
        return 1
} else {

        if ($timestamp <= $offline) {
            return 0;
        } else {
            return 1;


public function 
index($user_id NULL) {

    $udata $this->user_model->getuser($user_id);

    $data['active_status'] = $this->online($udata['last_loggedon']);

    $data['children'] = array(


There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!

I am not sure you can say if a user is currently online or not. What you can say, and do, is when they last logged in.

I suppose you could check if they have a current session, and assume if they do that they are online, but that is not true. A user can browse away or even shut down their computer and the session would still be active for a while. Only if users actually logout but most don't bother with that.

So as far as I know, there is no way to tell if a user is online right now or not.

You could add another column to your users datbase called last_activity and update that timestamp on every page request the user makes.

Theme © iAndrew 2016 - Forum software by © MyBB