Results 1 to 5 of 5

Thread: ip adress

  1. #1
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default ip adress

    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 Code:
    <?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.

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    Is there any way to make it only store one Ip adress per line?

    Thanks for any help

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •