Jump to content

online function


corillo181

Recommended Posts

i asked this question is the normal php helo but i think i will get better help here since the people coming in here know to deal with classes in specific

my question is if anyone could modify thi class what you think i'm missing or doing wrong.

<?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();
?>

Link to comment
https://forums.phpfreaks.com/topic/89939-online-function/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.