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;

}

}

 

}

 

Link to comment
Share on other sites

Diddnt think I needed that on an include, I tried it anyways and it still didnt work.

 

If I put a print $uid; directly before the class usersOnline{ it will print out the correct value, but anything within the class does not work.

Link to comment
Share on other sites

   $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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.