smith.james0 Posted April 16, 2012 Share Posted April 16, 2012 I have the following code is a included file on my main page class usersOnline { var $timeout = 600; var $count = 0; var $error; var $i = 0; function usersOnline () { $this->timestamp = time(); $this->ip = $this->ipCheck(); $this->new_user(); $this->delete_user(); $this->count_users(); $this->browser = $this->useragentbrowser(); $this->os = $this->useragentos(); } // start user function useragentbrowser(){ $agent = getenv('HTTP_USER_AGENT'); $browserArray = array( 'Windows Mobile' => 'IEMobile', 'Android Mobile' => 'Android', 'iPhone Mobile' => 'iPhone', 'Firefox' => 'Firefox', 'Google Chrome' => 'Chrome', 'Internet Explorer' => 'MSIE', 'Opera' => 'Opera', 'Safari' => 'Safari' ); foreach ($browserArray as $k => $v) { if (preg_match("/$v/", $agent)) { break; } else { $k = "Browser Unknown"; } } $browser = $k; return $browser; } function useragentos(){ $agent = getenv('HTTP_USER_AGENT'); $osArray = array( 'Windows 98' => '(Win98)|(Windows 98)', 'Windows 2000' => '(Windows 2000)|(Windows NT 5.0)', 'Windows ME' => 'Windows ME', 'Windows XP' => '(Windows XP)|(Windows NT 5.1)', 'Windows Vista' => 'Windows NT 6.0', 'Windows 7' => '(Windows NT 6.1)|(Windows NT 7.0)', 'Windows NT 4.0' => '(WinNT)|(Windows NT 4.0)|(WinNT4.0)|(Windows NT)', 'Linux' => '(X11)|(Linux)', 'Mac OS' => '(Mac_PowerPC)|(Macintosh)|(Mac OS)' ); foreach ($osArray as $k => $v) { if (preg_match("/$v/", $agent)) { break; } else { $k = "Unknown OS"; } } $os = $k; return $os; } // end user 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 ******(timestamp, ip, browser, os) VALUES ('$this->timestamp', '$this->ip', '$this->browser', '$this->os')"); if (!$insert) { $this->error[$this->i] = "Unable to record new visitor\r\n"; $this->i ++; } } function delete_user() { $delete = mysql_query ("DELETE FROM ***** 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 *******")); return $count; } } } The problem I have is that $browser and $os are not being inserted into the db, but everything else is. Can anyone help? Thanks James Link to comment https://forums.phpfreaks.com/topic/261022-code-not-work/ Share on other sites More sharing options...
Muddy_Funster Posted April 16, 2012 Share Posted April 16, 2012 I've never seen anyone use a foreach like that. try assigning the $k inside the loop instead of using a break. I don't know that you have access to $k / $v outside of the loop as they are defined within it's call. Link to comment https://forums.phpfreaks.com/topic/261022-code-not-work/#findComment-1337747 Share on other sites More sharing options...
smith.james0 Posted April 16, 2012 Author Share Posted April 16, 2012 If I use $visitors_online->useragentos() on my front page I get Mac OS so the code it working fine I just can't get it to insert into the db James Link to comment https://forums.phpfreaks.com/topic/261022-code-not-work/#findComment-1337772 Share on other sites More sharing options...
Muddy_Funster Posted April 16, 2012 Share Posted April 16, 2012 and what do you get if you echo $visitors_online->browser ? how and where are you actualy calling the usersOnline method? Link to comment https://forums.phpfreaks.com/topic/261022-code-not-work/#findComment-1337773 Share on other sites More sharing options...
smith.james0 Posted April 16, 2012 Author Share Posted April 16, 2012 and what do you get if you echo $visitors_online->browser ? I called it on index.php and got: Safari index.php <?php include_once ("usersOnline.class.php"); $visitors_online = new usersOnline(); if (count($visitors_online->error) == 0) { if ($visitors_online->count_users() == 1) { echo " There is " . $visitors_online->count_users() . " visitor online"; } else { echo " There are " . $visitors_online->count_users() . " visitors online"; } } else { echo "<b>Users online class errors:</b><br /><ul>\r\n"; for ($i = 0; $i < count($visitors_online->error); $i ++ ) { echo "<li>" . $visitors_online->error[$i] . "</li>\r\n"; } echo "</ul>\r\n"; } ?> James Link to comment https://forums.phpfreaks.com/topic/261022-code-not-work/#findComment-1337781 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.