Google Answers Logo
View Question
 
Q: Logging PHP Script ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: Logging PHP Script
Category: Computers > Programming
Asked by: theboistard-ga
List Price: $45.00
Posted: 29 Jul 2004 05:52 PDT
Expires: 28 Aug 2004 05:52 PDT
Question ID: 380702
I need a simple script that myself, being PHP clueless unable to setup properly.

I need to be as simple as possible (look at my other question),
without any extra HTML, any extra functions, without additional HTML
headers, messages or interface and so on. The simpler and shorter it
is, the better.

The script should do the following:

1. Log the Visitor IP to a txt file (one IP per line) appending it to
the END of the file.
2. If the text file is larger than X (lets say 1000) IP entries, then
it should update the whole list, removing the first entry and adding
the current/freshest one to the end -- ultimately updating the list
and keeping it at a constant X (1000) number of entries.
3. The simpler & shorter, the better
4. Please use unique variables, I would like to use this as an include
in another script and do not want the variables to interfere
5. If possible, do not use custom functions
6. If possible, both UNIX/win compatible
7. If possible, please test it on your own PHP server

Thank you
Answer  
Subject: Re: Logging PHP Script
Answered By: palitoy-ga on 29 Jul 2004 07:13 PDT
Rated:5 out of 5 stars
 
Hello theboistard

I have written the following script for you, if you have any questions
on it please ask for clarification and I will do my best to help.

You will need to add this script and a file called ip.txt to your
server.  You should CHMOD ip.txt to 777.

<? 
# name of the file that stores the ip addresses
$iplog_log_file = "ip.txt";

# read the ip address file into an array
$iplog_lines = file( $iplog_log_file );

# get the ip address of the current user
$iplog_ip = getenv('REMOTE_ADDR');

# add the ip address to the array
$iplog_lines[] = $iplog_ip; 

# count the number of lines in the array, if there are 
# more than 1000 then skip the first
if ( count($iplog_lines) >= 1000 ) { $iplog_start = 1; }
else { $iplog_start = 0; };

# prepare the data for output
for ( $iplog_i=$iplog_start; $iplog_i<1000; $iplog_i++ ) {
  if ( chop($iplog_lines[$iplog_i]) == "" ) { }
  else { $iplog_Data = $iplog_Data . chop($iplog_lines[$iplog_i]) . "\n"; };
}

// save the file
if($iplog_file=fopen($iplog_log_file, "w")) { // open file for writing
  fwrite($iplog_file, $iplog_Data);  // write to file
};
fclose($iplog_file);
?>

Clarification of Answer by palitoy-ga on 30 Jul 2004 07:45 PDT
Thanks for the 5-star rating and tip!  They are both appreciated.

Request for Answer Clarification by theboistard-ga on 06 Aug 2004 09:32 PDT
Alert!

I just noticed that the script doesnt keep the log at a constant 1000,
but once it reaches 1000 it RESETS back to 0!

Any ideas as to why ?

Clarification of Answer by palitoy-ga on 10 Aug 2004 06:08 PDT
Hello again

Sorry for not getting back to you sooner but I have been away on vacation.

I was unable to determine why the original version did not work as it
appeared to on my server.  Does it do this every time it gets to 1000
on your server?

Clarification of Answer by palitoy-ga on 10 Aug 2004 06:20 PDT
You could also try replacing these lines:

----------------------------------------------------------------------

# count the number of lines in the array, if there are 
# more than 1000 then skip the first
if ( count($iplog_lines) >= 1000 ) { $iplog_start = 1; }
else { $iplog_start = 0; };

# prepare the data for output
for ( $iplog_i=$iplog_start; $iplog_i<1000; $iplog_i++ ) {
  if ( chop($iplog_lines[$iplog_i]) == "" ) { }
  else { $iplog_Data = $iplog_Data . chop($iplog_lines[$iplog_i]) . "\n"; };
}

----------------------------------------------------------------------

with these:

----------------------------------------------------------------------

# count the number of lines in the array, if there are 
# more than 1000 then remove the first
if ( count($iplog_lines) > 1000 ) { array_shift($iplog_lines); };

# prepare the data for output
foreach ($iplog_lines as $iplog_value) {
   # for each value in the array add it to the $iplog_Data variable
   # for output
   $iplog_Data = $iplog_Data . chop($iplog_value) . "\n"; 
}

----------------------------------------------------------------------

This prints out the array holding the previous IP addresses.  If it is
larger than 1000 it chops the initial value of the array off.
theboistard-ga rated this answer:5 out of 5 stars and gave an additional tip of: $10.00
Its you, my PHP hero again!

Once again palitoy has done a great job in providing me with a quick
solution that I needed. I really appreciate the help, since I am PHP
clueless (ex: it took me half an hour to set up the script because I
changed a few variable names, and did not understand the write part)

Thank you so very much palitoy

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy