View Full Version : ip adress
keyboard
06-28-2011, 10:01 PM
Hi everyone,
On my old webserver they had a program which logged each visiters ip adress.
I searched the internet for php codes and found this
<?php
file_put_contents('ips.log', $_SERVER['REMOTE_ADDR'] . "\n", FILE_APPEND);
?>
Would this log each visitors ip adress. I know that cutting and pasting php codes can be dangerous so I just wanted to check if it does what it's meant to.
Thanks.
djr33
06-29-2011, 03:00 AM
That line uses the file_put_contents() function to add an IP log line to an existing text file. It will use the file named ips.log (in the current directory) and just add a line using the IP address.
What you have will work, but it will not be efficient. Every time the page is loaded it will add a new line with an IP address. This means that when you are testing your page your IP will show up repeatedly.
If you want to have a powerful IP logging system you will probably want to use a database. But you can do it like that if you want. You'll just have a long list of IPs.
You also might want to add some sort of activity information so you know what your visitors are doing.
I hope this helps you get started.
And yes, that code is completely safe. Make sure the file ips.log exists and is not protected from being edited.
keyboard
06-29-2011, 10:36 PM
Is there any way to make it only store one Ip adress per line?
Thanks for any help
djr33
06-29-2011, 11:16 PM
I think that should store one per line. The "\n" part means "line break". If you replace that with "\r\n" it may be compatible with more systems. (Windows, Mac and Linux all use different combinations of \r and/or \n.)
If you mean one unique IP each line (so there are no duplicates) that's hard. It would be possible to do this using the text file alone and splitting it each time, resorting all of the IPs, then combine duplicates to a single line. HOWEVER, that would only work for a short list because once you had a lot of visitors it would start to severely slow down the page, every time it loaded. So if you need this, I very strongly suggest looking into a database. Short of that you could create a directory of IP logs, and create a separate file for every IP address, titled 1.2.3.4.log (for example). That would be somewhat easier to organize (in the PHP) though harder to read as an actual log.
djr33
06-29-2011, 11:16 PM
I think that should store one per line. The "\n" part means "line break". If you replace that with "\r\n" it may be compatible with more systems. (Windows, Mac and Linux all use different combinations of \r and/or \n.)
If you mean one unique IP each line (so there are no duplicates) that's hard. It would be possible to do this using the text file alone and splitting it each time, resorting all of the IPs, then combine duplicates to a single line. HOWEVER, that would only work for a short list because once you had a lot of visitors it would start to severely slow down the page, every time it loaded. So if you need this, I very strongly suggest looking into a database. Short of that you could create a directory of IP logs, and create a separate file for every IP address, titled 1.2.3.4.log (for example). That would be somewhat easier to organize (in the PHP) though harder to read as an actual log.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.