Help with ip logger

Hello everyone,

I recently have been trying to develop a php script that would record ips of people who have visited my site. The script needs to not only log the ips into a file, but I dont want it to log the ips more than once. For example, if 12.34.56.78 is in the file and someone with that ip goes to my site, it should not record it again.

Here is my script so far:

[code]<?php

$ip = getenv(‘REMOTE_ADDR’);
$message = “$ipn”;
$file123 = ‘log.txt’;
$pos = strpos($file123, ‘$ip’);

if ($pos === false) {
$Open = fopen($file123, “a”);
fwrite($Open, “$message”);
fclose ($Open);
} else {
break;
}[/code]

I really dont see whats wrong. It writes everything to the file fine, but it always returns the conditional statement as false (meaning it records the ips more than once.

If someone could help me figure this out, than that would be great. I also need a way to clear the file every 24 hours. If you know how to do that as well then it would be greatly appreciated.

Thanks,
~Thorlax402

in your code you have:

[php]

<?php $ip = getenv('REMOTE_ADDR'); $message = "$ipn"; $file123 = 'log.txt'; $pos = strpos($file123, '$ip'); if ($pos === false) { $Open = fopen($file123, "a"); fwrite($Open, "$message"); fclose ($Open); } else { break; } [/php] it's not your conditional that's the problem, its your $pos that's the problem. Before the conditional, you assign $pos the value from the strpos function. The strpos function looks for the string from $ip in the string from $file123. In your case $file123 is not the file itself rather the NAME of the file (as a string. This makes you code work like this (see comments in code): [php] <?php // // For the sake of this explanation assume an ip address of 192.168.254.1 // // I would prefer to use // $ip = $_SERVER['REMOTE_ADDR'] // as I feel it's more secure. $ip = getenv('REMOTE_ADDR'); // get's ip Address and assigns to $ip $message = "$ipn"; // Assings the IP value to $message and puts a newline on it. $file123 = 'log.txt'; // Assigns the "Literal" text name of log.txt to the variable $file123 // IMPORTANT PART HERE $pos = strpos($file123, '$ip'); // Literally the above is the same as $pos = strpos('log.txt', '192.168.254.1'); // since both of the preceding statements are FALSE as 192.168.254.1 cannot be found in log.txt // the value of $pos is assigned a false (and will ALWAYS be false since // the IP Address is NEVER in the literal "log.txt" string // Given the above about $pos the below is akin to a literal // IF (FALSE equals FALSE) ...... // Thus the "Conditional" evaluates to TRUE every time. if ($pos === false) { $Open = fopen($file123, "a"); fwrite($Open, "$message"); fclose ($Open); } else { // Never gets here because the above will ALWAYS be true. break; } [/php]

Oh…I understand. It wasn’t actually searching the file, it was searching the name of the file. If that is the case, how do I get it to seach the file contents instead?

Have you considered using a database instead??

It would be much more efficient in the long run.

I will take that into consideration for now, but this whole thing is more for learning purposes for me. I’m trying to learn to write custom php scripts and just needed some guidance. After I complete this project, I may work to learn to use databases too.

To get the contents of a file without having the hassle of fopen(), fclose(), etc, you could take a look at file() or file_get_contents(), whichever works best in your application.

Ok, I altered the script with the file_get_contents() function, but it is still not working or returning any errors:

[code]<?php

$ip = $_SERVER[‘REMOTE_ADDR’];
$message = “$ipn”;
$file123 = ‘log.txt’;
$pos = strpos($file21, ‘$ip’);
$file21 = file_get_contents(“log.txt”);

if ($pos === false) {
$Open = fopen($file123, “a”);
fwrite($Open, “$message”);
fclose ($Open);
} else {
break;
}

?>[/code]

That’s because you’re still using the string ‘log.txt’ to compare. Have you looked up file_get_contents() in the PHP manual? Do you know where to put it in your script?

I looked it up in the php manual and it looked as though it made the contents of a file into a string. If that is true than shouldn’t my strpos function show $file21 as that string (the contents of the file)?

Yep, but not until you actually call that function. The reason it doesn’t work is because you’re checking the value first, and only then assign the contents of the log file to it.

So is my order of function placement wrong? How do I fix it so that that is called first?

as u said. cange the order:
[php]
$file21 = file_get_contents(“log.txt”);
$pos = strpos($file21, $ip);
[/php]

P.S.: don’t use ‘$ip’ (that is the string ‘$ip’), just use $ip (thats the content of $ip).
“$ip” is the same as $ip, but ‘$ip’ isn’t. take a look at http://php.net/manual/en/language.types.string.php

Thank you…I had already tried changing the order to see if it would work, but it turns out with that and the removing of the quotes, it works fine now.

Thanks

Ok, so I got everything working properly, but now I am trying to get the script to only show the last x number of ip addresses in the file. Here is what I have:

[code]<?php

$ip = $_SERVER[‘REMOTE_ADDR’];
$message = “$ipn”;
$file123 = ‘log.txt’;
$file21 = file_get_contents(“log.txt”);
$pos = strpos($file21, $ip);

if ($pos === false) {
$Open = fopen($file123, “a”);
fwrite($Open, “$message”);
fclose ($Open);

$contents=file(‘log.txt’);

if (count($contents) > 3) {
array_shift($contents);
}

} else {
exit;
}

?>[/code]

It looks as though it should bump off the first ip address when there gets to be more than 3 ip addresses, but it doesn’t. I know my 2nd if statement looks a little weird, but I didn’t know where to put it. If someone could help that’d be great.

P.S. An alternative to this would be to clear the file every day. If someone knows how to do that then I would appreciate that as well.

You can clear the file one of 2 ways…

  1. Delete the file using Unlink() function then recreate it using the touch() function.

  2. or you can just clear it by doing fopen(“filename.txt”, “w”) and then using fwrite() and just write nothing to the file. Something like:

$contents = “”;
fwrite($handle, $contents);

This should clear the file.

if i got you right. you try to modify the file with array_shift. but that only modifies the array. you have to write the modified array back to the file.
[php]
if (count($contents) > 3)
{
array_shift($contents);

//add this:
if($Open = fopen($file123, “w”))
{
foreach($contents as $line)
{
fwrite($Open, $line);
}
fclose ($Open);
}
}
[/php]

Ok, thank you to everyone that responded in this thread. I managed to make an ip logger that logs the ips into a text file, but doesn’t record any ip more than once. I also got it so that it clears the file every 24 hours (or however long I want it to with simply edits). Here is the final script for anyone that may need it:

log.php

[code]<?php

$ip = $_SERVER[‘REMOTE_ADDR’];
$message = “$ipn”;
$file123 = ‘log.txt’;
$file21 = file_get_contents(“log.txt”);
$pos = strpos($file21, $ip);
$date = date(G);

if ($pos === false) {
$Open = fopen($file123, “a”);
fwrite($Open, “$message”);
fclose ($Open);
} else {
}

if ($date == “0”) {
$Open = fopen($file123, “w”);
fwrite($Open, “”);
fclose ($Open);
} else {
}
?>[/code]

It writes to the file “log.txt” (needs to be created with permissions edited).
Thanks again!

Sponsor our Newsletter | Privacy Policy | Terms of Service