CodeIgniter Forums
Is my online function OK or does it need improvment? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Is my online function OK or does it need improvment? (/showthread.php?tid=67467)



Is my online function OK or does it need improvment? - wolfgang1983 - 02-25-2017

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.

http://localhost/project/user/1


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(
        'common/header',
        'common/footer',
        'common/navbar' 
    );

    $this->load->render('user/user'$data);





RE: Is my online function OK or does it need improvment? - PaulD - 02-25-2017

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.


RE: Is my online function OK or does it need improvment? - Diederik - 02-25-2017

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