corillo181 Posted February 7, 2008 Share Posted February 7, 2008 can anyone tell me the best way to track a member? i have a database name users online one with counter and i got a class setup that is in ever page to keep track of the member. problem 1. using ip, cell phone users give out a new one every im they click on a new page so that wasn't working. problem 2. using session_id . i thought this was the best choice but someone cell phones are giving me some problem. Quote Link to comment https://forums.phpfreaks.com/topic/89925-member-tracking/ Share on other sites More sharing options...
phpSensei Posted February 7, 2008 Share Posted February 7, 2008 Here was my help back in the day for this question, so use this function <?php function online_users($period = 300 /* seconds */) { $query = "DELETE FROM `online_users` WHERE `updated` < '".(($now = time()) - $period)."'"; $result = mysql_query($query) or die ($query.' -- '.mysql_error()); $query = "INSERT INTO `online_users` (`ip`, `updated`, `user`) ". "VALUES ('".$_SERVER['REMOTE_ADDR']."', '".$now."', '".(isset($_COOKIE['user'])?1:0)."')". "ON DUPLICATE KEY UPDATE `updated`='$now', `user` = '".(isset($_COOKIE['user'])?1:0)."'"; mysql_query($query) or die ($query.' -- '.mysql_error()); $query = "SELECT * FROM `online_users`"; $result = mysql_query($query) or die ($query.' -- '.mysql_error()); $return = array('users' => 0, 'guests' => 0); if(mysql_num_rows($result) > 0) { while($row = mysql_fetch_assoc($result)) { if($row['user'] == 1) { $return['users']++; } else { $return['guests']++; } } } return $return; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/89925-member-tracking/#findComment-460994 Share on other sites More sharing options...
phpSensei Posted February 7, 2008 Share Posted February 7, 2008 Cant modify my posts ATM, but heres a desc: Here was my help back in the day for this question, so use this function You insert all the current username sessions evertime the user refreshes the page, but before that you have to delete the old usernames from your db that at 5 minutes old. Lets say bob came on the site and his name is on the user online list, and so is Sam, now 5 minutes later Bob's session is expired and you delete all the old username sessions from the db that are 5 minutes old, but sam is still online, so when he refreshes the page his username will be in the user online table, but bob's wont... and this process continues... Quote Link to comment https://forums.phpfreaks.com/topic/89925-member-tracking/#findComment-460998 Share on other sites More sharing options...
corillo181 Posted February 7, 2008 Author Share Posted February 7, 2008 i already have a code. is there anything that you would add to it or take out. <?php class online{ public $remote; public $ref; public $uagen; public $on; public $db; public $sid; public $uid; public function __construct($r_add,$h_ref,$h_agent,$sid){ $this->remote = $r_add; $this->ref = $h_ref; $this->uagent = $h_agent; $this->sid = $sid; $this->db = new DB(); } /* set the session of the members online. $on = $_SESSON[''] user session identifier $uid = user id if NULL is a guess */ public function setOnline($on){ $this->on = $on; //get the time current time $time = time(); //check to see if the session is empty if(empty($this->on)){ // check to see if the session of this user was already in the database $query = "SELECT session_id FROM table WHERE session_id='{$this->sid}'"; $result = $this->db->query($query); $count = $this->db->num_rows($result); //if the session id is in the database the user closed the borswer and came back //just add the session last 5 minutes if($count>0){ $_SESSION['on'] = 'online'; }else{ /* if no session id in the database then add the user set the session and update the new visitors table */ $query = "INSERT INTO `table` (`session_id` ,`activity` ,`ip_address` ,`refurl` ,` user_agent`)VALUES ('{$this->sid}','$time','{$this->remote}','{$this->ref}','{$this->uagent}')"; $this->db->query($query); $query2 = "UPDATE tra_visits SET count=(count+1)"; $this->db->query($query2); $_SESSION['on'] = 'online'; } } } /* checks to see if the user has in id not null a member null = guest */ public function setUser($uid=NULL){ $this->uid = $uid; if($this->uid){ $query = "UPDATE table SET activity='$time', member='y' WHERE session_id='".$this->sid."'"; $this->db->query($query); return true; } } /* updates a user field if exist everytim the user enters a new page. */ public function updateOnline($on){ $time = time(); if($on=='online'){ $query = "UPDATE table SET activity='$time', refurl='{$this->ref}' WHERE session_id='{$this->sid}'"; $result = $this->db->query($query); } } /* checks to see if a field is not active for more than 5 minutes and deletes it. */ public function notOn(){ $time = time(); $query = "DELETE FROM table WHERE ('$time'-activity)>'500'"; $this->db->query($query); } } $remote = $_SERVER['REMOTE_ADDR']; $ref = $_SERVER['HTTP_REFERER']; $uagent = $_SERVER['HTTP_USER_AGENT']; $sid = session_id(); $on = $_SESSION['on']; $uid = $_SESSION['user_id']; $online = new online($remote,$ref,$uagent,$sid); $online->setOnline($on); $online->setUser($uid); $online->updateOnline($on); $online->notOn(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/89925-member-tracking/#findComment-461075 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.