Log Stats in PHP
Welcome to the second part of this mini tutorial /
code snippet . Now we will log some info to our MySQL
database using PHP .
Here is the script we are using , you will have to
input ypour own host , username and password for this
to operate correctly.
<?php
//store the users browser in a variable
$users_browser = $HTTP_USER_AGENT;
//store the users IP in a variable
$users_ip = $REMOTE_ADDR;
//database connection
$connection = mysql_connect("localhost" ,
"username" , "password");
//select the stats database
mysql_select_db("stats" , $connection);
//insert the info into the stats table
$sql_query = "INSERT INTO stats(browser,ip,logdate)
VALUES('$users_browser','$users_ip',now())";
//store
$result = mysql_query($sql_query);
?>
Now all we have to do is test the script works correctly
, save the above and call it something like logstats.php
, now load this page into your web browser , if you
have more than one different make of browser that is
even better .
Now go back to MySQL command line and type in the following
SELECT * FROM stats;
In the next part we will show how to display the results
in an HTML table in your browser.
|