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; } } } Link to comment https://forums.phpfreaks.com/topic/58699-solved-variable-not-passing-to-included-class/ 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 Link to comment https://forums.phpfreaks.com/topic/58699-solved-variable-not-passing-to-included-class/#findComment-291147 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? Link to comment https://forums.phpfreaks.com/topic/58699-solved-variable-not-passing-to-included-class/#findComment-291193 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. Link to comment https://forums.phpfreaks.com/topic/58699-solved-variable-not-passing-to-included-class/#findComment-291220 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. Link to comment https://forums.phpfreaks.com/topic/58699-solved-variable-not-passing-to-included-class/#findComment-291224 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 Link to comment https://forums.phpfreaks.com/topic/58699-solved-variable-not-passing-to-included-class/#findComment-291230 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.