View Full Version : Recording to text file

Len Whistler
09-28-2003, 06:46 PM

When ever a visitor goes to my site I would like a .php script write to a text (.txt) file with the following information about the visitor:

IP address
Browser and OS
Their local time

I then can go to the text file and take a look at the info. If someone can help that would be great. Thanks

Leonard Whistler

09-29-2003, 03:31 AM
Untested and just written in here, but it should work

(see http://php.net/fwrite for more examples)

The file you need to create and chmod to 0777 is called log.txt and put the php below into your php page

$filename = "log.txt";

$somecontent = $_SERVER['REMOTE_ADDR']." :: ".$_SERVER['HTTP_USER_AGENT']." :: ".date ("l dS of F Y h:i:s A").

// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {

// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle = fopen($filename, 'a')) {
print "Cannot open file ($filename)";

// Write $somecontent to our opened file.
if (!fwrite($handle, $somecontent)) {
print "Cannot write to file ($filename)";

print "Success, wrote ($somecontent) to file ($filename)";


} else {
print "The file $filename is not writable";

Getting their timezone is a little tricky, see the thread of zoobie's for more info http://www.codingforums.com/showthread.php?threadid=26898

The time put on the file is the time from your server, not their time, unless they're in the same timezone as your server is.

Len Whistler
09-29-2003, 07:29 AM
Nightfire..thanks for the help. :)

Leonard Whistler