Shifu-Hacks Blog

愚かさは博学ではない。

Logging Users (PHP)

Posted by compactwater on October1 2007

A simple small code to include in any page to log users on your server.
Use protection on log files! Do not let them become public! You can be sued for keeping unprotected logs… But unlikely unless you’re some big company.

<?php
$logname = “logs/users.” . date(“Ymd”) . “.log”;
$user = $_SERVER[‘REMOTE_ADDR’];
$from = $_SERVER[‘HTTP_REFERER’];

if (!$from == “”) {
$toput = “Time: ” . time() . ” IP: ” . $user . ” by: ” . $from . ”
“;
} else {
$toput = “Time: ” . time() . ” IP: ” . $user . ”
“; }

$handle = fopen($logname, ‘a’);
if (fwrite($handle, $toput) === FALSE) {
echo “Error writing to ” . $logname;
exit;
}
fclose($handle);
?>

Put that into some .PHP file, and upload it to your server (if necessary). Add this:
<?php
include ‘log_my_users.php’;
?>
to all the pages you want to log users on. I recommend having a re-direct front page with this, or using cookie or IP identification, so you don’t log a user more than once on the front page.

Note: You will obviously need PHP on you server, and the folder “logs” in the same directory. You can change the folder name, and even move it to somewhere off of your server, but just remember to keep it safe from other users that may want to use it for the wrong reasons.

Advertisements

2 Responses to “Logging Users (PHP)”

  1. Wonderful paintings! That is the kind of information that are meant to be
    shared across the net. Disgrace on Google for not positioning this put up upper!
    Come on over and visit my site . Thank you =)

  2. Hi there everyone, it’s my first pay a visit at this website, and article is in fact fruitful in favor of me, keep
    up posting such content.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: