Archive for September, 2011

CakePHP Online Now With User History Traker

Its very simple to have a online block on your webpage in CakePHP.
Here I am going to explain you Step By Step Tutorial for this. I have just developed it so want to share with Everyone because i have done research on google from long hours.

TriCore IT Solutions
Step 1 :Hit The Controller first.
Create a function in your app_controller. You can create it anywhere in the controller.

function recordActivity()
			$this->Activity->query("DELETE FROM activities  WHERE user_id = '".$this->Auth->user('id')."'");
			$this->data['Activity']['user_ip'] = $_SERVER['REMOTE_ADDR'];
			$this->data['Activity']['user_id'] = $this->Auth->user('id');
			$this->data['Activity']['user_browser'] = $_SERVER['HTTP_USER_AGENT'];
			$this->data['Activity']['user_accessed'] = date("Y-m-d H:i:s");

Here you can see that i have used “Activity” model that’s having User’s IP Address , User’s Logged in ID
The browser from which user has requested the webpage and Last Accessed Date>

The below line is used to unset the Activity model because we have declared our function in application controller not
in “Activity” Controller. If we Declare it in “Activity” Controller then it will be auto reset.


Step 2 : Now call your defined function into same “app_controller” inside public function beforeFilter()

public function beforeFilter() {

Step 3 : Create a table in your database name as “activities” it will track logs of users and by using this tabel we will display the “Online Users”.

CREATE TABLE `activities` (
  `activity_id` int(11) NOT NULL auto_increment,
  `user_browser` varchar(255) NOT NULL,
  `user_ip` varchar(15) NOT NULL,
  `user_id` varchar(15) NOT NULL,
  `user_accessed` datetime NOT NULL,
  PRIMARY KEY  (`activity_id`)

Step 4 : We are done with tracking logs of users data you can check them into your database.
Now lets suppose you want to show the logged in users on home page of in side block of your website then just find them out from “Activity” and “User” Models..

$online_cond = array(
								'limit' => 10,
								'conditions' => array(' activities.user_accessed  >= DATE_SUB( CURRENT_DATE() , INTERVAL 10 MINUTE) ' ),
								'order' => array(' activities.user_accessed DESC ' ),
								'group' => ' ',
								'joins' => array(
           								'table' => 'activities',
           								'alias' => 'activities',
           								'type' => 'LEFT',
           								'conditions'=> array(' activities.user_id  = ')
		$OnlineUser_Array = $this->User->find('all' , $online_cond);
		$this->set('OnlineUser_Array', $OnlineUser_Array);

Step 5 : You must delete log of user who is going to logged out from the system. It will delete his logged history.
This Step is necessary for only those who are not looking to keep track of logged user.
The developers who are only looking for “Online User” Functionality can put below code to reduce database weight.

public function logout() {
        $this->Session->setFlash(__('Log out successful.', true), 'default', array('class' => 'success'));
		$this->Activity->query("DELETE FROM activities  WHERE user_id = '".$this->Auth->user('id')."'");

You Have done it :) Check Your page and Enjoy the time you saved …
Hire Me
Follow Me!
Most Popular Articles & Pages
Because your vote is Important
Sorry, there are no polls available at the moment.