A simple PHP / MySQL function to log site visits/hits
| I just wanted to share a simple php logging function I wrote. The function is primitive as it records every accessed instance of a page it is called on, regardless of session time etc…It has a lot of room for expansion, and can be easily used in conjunction for hit stats etc…
First of all we want to create a Table in our database to record values we want, so goto your MySQL database, phpmyAdmin or whatever your preference and create the table described here. CREATE TABLE `kind_log` ( `log_id` int(11) NOT NULL auto_increment, `rem_ip` varchar(100) NOT NULL, `username` varchar(100) NOT NULL, `usr_agent` varchar(100) NOT NULL, `time` datetime NOT NULL, PRIMARY KEY (`log_id`) ) After you create the database table you then need to create a functions.php, name it whatever you’d like as long as you remember it so you can include it in any of the subsequent pages you’ll be using the function. Usually most content management systems or CMS’s already have a pre-defined functions page. Function Code: function ip_log() { $ip = $_SERVER['REMOTE_ADDR']; $agent = $_SERVER['HTTP_USER_AGENT']; $usr = 'Surfer Dude'; mysql_query(“INSERT INTO kind_log(rem_ip, username, usr_agent, time) VALUES (‘$ip’, ‘$usr’, ‘$agent’, NOW())”); } Now to use this function simply declare ip_log(); somewhere in your site, like in the top of your header, make sure to include the function file that it is located in however or the script wont work. When the function passes during a visit it will record the users IP address($ip), the User Agent($agent) (ie: Internet Explorer, Mozilla Firefox etc…) , a name association ($usr) (which can be set to a logged in username from a SESSION value) and the time of the “hit” within the database. Courtsey:Circuitbomb @ flyninja. |

