Hello and welcome to our community! Is this your first visit?
Register
Results 1 to 5 of 5
  1. #1
    Regular Member
    Join Date
    Jan 2010
    Posts
    245

    PHP IP Logger V1.2

    Some credits go to a friend of mine that gave me the idea.

    Code:
    <?php
    $lf = "my_records.html"; 
    $IP = $REMOTE_ADDR;
    $x = $_SERVER['REQUEST_URI'];
    If (getenv(HTTP_CLIENT_IP)) {
    	$IP = getenv(HTTP_CLIENT_IP);
    } else {
    	$IP = getenv(REMOTE_ADDR);
    	}
    $date_time = date("l j F Y  g:ia", time() - date("Z")) ;
    $fp = fopen("$lf", "a");
    fputs ($fp,"+---------------------
    Date: $date_time
    Client IP Address: $IP[b]Visited Page: $x
    +---------------------
    ");
    flock($fp, 3); 
    fclose($fp);
    ?>
    V1.2
    Code:
            <?php
            $x = $_SERVER['REQUEST_URI'];
            $IP = $_SERVER['REMOTE_ADDR'];
            $date_time = date("l j F Y  g:ia", time() - date("Z")) ;
             mysql_connect("MYSQL_HOST", "USERNAME", "PASSWORD") or die(mysql_error());
             mysql_select_db("DATABASE") or die(mysql_error());
    
             mysql_query("INSERT INTO IPLOGS (ip, page, date) VALUES('".$IP."', '".$x."', '".$date_time."' ) ")
                                                                                        or die(mysql_error()); 
            ?>
    Just put that into your server and on the pages you want, add:

    Code:
    <?php include("ip.php"); ?>
    Or whatever the name of the file is. Enjoy!
    [center:2lrplsjz][/center:2lrplsjz]

  2. #2
    Obsessed Veteran Adam's Avatar



    Join Date
    Nov 2004
    Location
    UK / England
    Posts
    17,271

    Re: PHP Simple IP Logger

    Some CC.

    1. getenv is not a reliable way of grabbing a users IP it's behaviour can differ over platforms. Use $_SERVER.
    2. Writing to file everytime a visitor hits a page is slow. If a page has high traffic your script can potentially make the server fallover.

  3. #3
    Regular Member
    Join Date
    Jan 2010
    Posts
    245

    Re: PHP Simple IP Logger

    Thank you for the positive CC, Adam. What do you recommend to fix this?
    [center:2lrplsjz][/center:2lrplsjz]

  4. #4
    Obsessed Veteran Adam's Avatar



    Join Date
    Nov 2004
    Location
    UK / England
    Posts
    17,271

    Re: PHP Simple IP Logger

    Use $_SERVER['REMOTE_ADDR']; to get a users ip. Construct a simple database to record stats not files. To clarify I'm not saying using files in all cases is bad. But in this situation if you have X people writing to file you can hinder server performance, not to mention your likely to drop some statistical data in the process.

  5. #5
    Regular Member
    Join Date
    Jan 2010
    Posts
    245

    Re: PHP Simple IP Logger

    Yeah, I see what you mean. This should be a slight improvement, no?

    Code:
        <?php
        $x = $_SERVER['REQUEST_URI'];
        $IP = $_SERVER['REMOTE_ADDR'];
        $date_time = date("l j F Y  g:ia", time() - date("Z")) ;
         mysql_connect("MYSQL_HOST", "USERNAME", "PASSWORD") or die(mysql_error());
         mysql_select_db("DATABASE") or die(mysql_error());
    
         mysql_query("INSERT INTO IPLOGS (ip, page, date) VALUES('".$IP."', '".$x."', '".$date_time."' ) ") 
                                                                                    or die(mysql_error());  
        ?>
    It sure is smaller, I like it lol.
    [center:2lrplsjz][/center:2lrplsjz]


 

Posting Permissions

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