Jump to content

[SOLVED] Variable not passing to included class


devknob

Recommended Posts

Please excuse my ignorance as this is day 6 on learning php, and this is the first time I ever use "function" and "classes"

Ive spent like 4 hours ??? trying to figure out why my variable $uid is not passed from profile.php to my usersOnline.class.php (see where the SQL insert refers to $this->un

 

My end-goal is to basically have the "This user is online" on a users profile like myspace, so if there is a better solution, please post it.

 

PHP version 5.1.6

MySQL version 5.0.27-standard-log

 

it goes something like this

$uid = $_SESSION['uid']

include('classUsers.class.php')

 

class usersOnline {

 

var $timeout = 600;

var $count = 0;

var $error;

var $i = 0;

 

function usersOnline () {

 

$this->un = $uid;

$this->timestamp = time();

$this->ip = $this->ipCheck();

$this->new_user();

$this->delete_user();

$this->count_users();

}

 

function ipCheck() {

 

if (getenv('HTTP_CLIENT_IP')) {

$ip = getenv('HTTP_CLIENT_IP');

}

elseif (getenv('HTTP_X_FORWARDED_FOR')) {

$ip = getenv('HTTP_X_FORWARDED_FOR');

}

elseif (getenv('HTTP_X_FORWARDED')) {

$ip = getenv('HTTP_X_FORWARDED');

}

elseif (getenv('HTTP_FORWARDED_FOR')) {

$ip = getenv('HTTP_FORWARDED_FOR');

}

elseif (getenv('HTTP_FORWARDED')) {

$ip = getenv('HTTP_FORWARDED');

}

else {

$ip = $_SERVER['REMOTE_ADDR'];

}

return $ip;

}

 

function new_user() {

$insert = mysql_query ("INSERT INTO useronline(timestamp, ip, un) VALUES ('$this->timestamp', '$this->ip', '$this->un')");

if (!$insert) {

$this->error[$this->i] = "Unable to record new visitor\r\n";

$this->i ++;

}

}

 

function delete_user() {

$delete = mysql_query ("DELETE FROM useronline WHERE timestamp < ($this->timestamp - $this->timeout)");

if (!$delete) {

$this->error[$this->i] = "Unable to delete visitors";

$this->i ++;

}

}

 

function count_users() {

if (count($this->error) == 0) {

$count = mysql_num_rows ( mysql_query("SELECT DISTINCT ip FROM useronline"));

return $count;

}

}

 

}

 

   $this->un = $_SESSION['uid'];

 

If you assign $_SESSION['uid'] to $uid, $uid is not a global variable, where as $_SESSION is. Meaning that you cannot use $uid outside of the scope, where the class would be outside of the scope. Change the line above and it should work.

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.