Local Tech Repair: keep track of who access your site.

Wednesday, January 26, 2011

keep track of who access your site.

if you have a login based site and your wanting to keep track of who logs in and where they are login in from. here is a simple script that helps you do this.
you can also use this for users to be able to see where they have logged in from so they know if someone else is login in using their name.

------------script--------
<?php
include "../connect.php";   //brings in connection information
$ip = $_SERVER['REMOTE_ADDR'];  // grabs the users IP address
$username = $_COOKIE['Username'];  // checks for the username probably be smarter to use a session so the cookie does not get deleted
$date = date('Y-m-d H:i:s');  // grabes the server time
$sql = "insert into loginHistory (ip, username, datetime) VALUES ('$ip','$username','$date')"; // insert query
$result=mysql_query($sql); // finally inserts it.
?>
-------------------------------


from here if you wanted you display it in different manners how ever you want to.

for instance below you could do...


--------------script2------------------
include ("../connect.php");
// Build SQL Query
$sql = 'SELECT * FROM `loginHistory` ORDER BY `id` DESC LIMIT 0, 20 ';
$numresults=mysql_query($sql);
$count = 1 + $s ;
print " <table border='1' cellspacing='3' cellpadding='2'>";
// now you can display the results returned
print "<tr><td>Count</td><td>IP Address</td><td>User</td><td>Date and Time</td></tr>";
while ($row= mysql_fetch_array($numresults)) {
$title = $row["IP"];
$title2 = $row["username"];
$title3 = $row["datetime"];
print "<tr><td>";
echo "$count";
print "</td><td>";
echo "$title";
print "</td><td>";
echo " $title2" ;
print "</td><td>";
echo " $title3" ;
print "</td></tr>";
$count++ ;
}
print "</table>";

?>
--------------------------------------------------------
what this does is create a table and then fill it with a count, ip address, username, and date time that they last checked the page.

warning if you have a lot of members this script can put a lot of stress on your system because this will log each time the user loads a page or what ever page you put this on... if you put this on the login you will not have that problem though. it will only log once.

No comments:

Post a Comment