Click here to Skip to main content
11,410,750 members (62,737 online)
Click here to Skip to main content

Tagged as

Check Member's/ User's Online or Offline Status

, 11 Dec 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
The following piece of code will calculate the last visited time of a Member/User and calulate the time difference with the current time.

Check Members online offline CAKEPHP


The following piece of code will calculate the last visited time of a Member/User and calculate the time difference with the current time. And if the Member/User's last visited time is greater than 10 minutes then that will be considered OFFLINE status, otherwise ONLINE.


I was just searching for a time difference code in Bakery but couldn't find any simple code. I developed and used the following code to calculate the time a User/Member is online or offline.

Using the code

There are only three steps to follow. This code is only for CakePHP 1.5.

"I am assuming that you have a table named MEMBER (you can customize it according to your requirements)".

  1. Make a field last_visit (DATETIME) in MEMBER table.
  2. For app controller:
  3. // " Component Class "
    class AppController extends Controller {        
        * beforeRender() function will run on every visit of the website
        function beforeRender()
            ### UPDATE LAST VISIT ###
            $online_offline_status = 0;
            if ($this->Session->check('userId')==true){
            // Checking for the SESSION - Proceed only if MEMBER/USER is logged in.
                $this->loadModel('Member'); // Loading MEMBER Model
                // UPDATE MEMBER VISIT TIME
                $last_visit = date('Y-m-d H:i:s', time());
                $this->Member->updateAll(array('Member.last_visit' => '"'.$last_visit.'"'), 
                                array('' => $this->Session->read('userId')));
                // GET TIME DIFFERENCE
                $member_last_visit = $this->Member->find('first', array('conditions' => 
                   array('' => $this->Session->read('userId'))));
                $current_time = strtotime(date("Y-m-d H:i:s")); // CURRENT TIME
                $last_visit = strtotime($member_last_visit['Member']['last_visit']); // LAST VISITED TIME
                $time_period = floor(round(abs($current_time - $last_visit)/60,2)); //CALCULATING MINUTES
                //$time_period = floor(round(abs($current_time - $last_visit)/60,2)/1440);
                echo $time_period;
                if ($time_period <= 10){
                    $online_offline_status = 1; // Means User is ONLINE
                } else {
                    $online_offline_status = 0; // Means User is OFFLINE
            $this->set('online_offline_status', $online_offline_status);
        }//end beforeRender()            
  4. Simply use the following code in your .ctp file according to your demand.
  5. // " View Template "
    <!-- IF USER IS OFFLINE -->
    if ($online_offline_status == 0){
      echo '(Member/User is Offline)';
    <!-- IF USER IS OFFLINE -->
     } else if ($online_offline_status == 1) {
      echo '(Member/User is Online)';


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Chief Technology Officer HaiTechnologies
Pakistan Pakistan
Love Web Applications Development

MVC, Web Scrapping, Webservices, CAKE PHP, WordPress, Joomla, Drupal, Magento, OsCommerce, Paypal,, XML, CSS, DOM, HTML, AJAX, JavaScript, Jquery, Custom CMS or content management system development, E-commerce, ERP application development, XML, XSLT, MySql, SQL Server 2008 R2, 3rd party libraries, Opencart, Cubecart, Adobe PhotoShop CS5, basic knowledge of Flash. Google API, FBML, Web 2.0

Comments and Discussions

-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150414.5 | Last Updated 11 Dec 2012
Article Copyright 2012 by imran.haitechnologies
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid