devknob Posted July 6, 2007 Share Posted July 6, 2007 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; } } } Quote Link to comment Share on other sites More sharing options...
devknob Posted July 6, 2007 Author Share Posted July 6, 2007 btw, everything below the include IS the include Quote Link to comment Share on other sites More sharing options...
per1os Posted July 6, 2007 Share Posted July 6, 2007 Did you put session_start() at the top of each page you are trying to access or set the session variable? Quote Link to comment Share on other sites More sharing options...
devknob Posted July 6, 2007 Author Share Posted July 6, 2007 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. Quote Link to comment Share on other sites More sharing options...
per1os Posted July 6, 2007 Share Posted July 6, 2007 $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. Quote Link to comment Share on other sites More sharing options...
devknob Posted July 6, 2007 Author Share Posted July 6, 2007 thank you 1,000,000 times buddy Quote Link to comment 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.