IP Logger Q&A

Discussion in 'PHP' started by keith51, Oct 12, 2007.

  1. #1
    Hi Guys i run a little script on a local forum to log IP's for various reasons but im getting bogged down with IP's i dont want to see on the log ( like my own or the regular bot crawls )
    Is there a way for me to adjust the script below to " Not " log certain Ip's but not ban them from using my site .

    I am slowly learning the rudiments of PHP and have tried a few times and failed so rather than lose heart i thought i would ask to be put right .:)

    Keith
    ------------------------------------------------------------------

    <?php

    define("LOG_FILE","visitors.txt");

    $logfileHeader=' Forum Visitors Log;

    IP NAME (add 1 hour) WHO IN FROM '."\n";

    $userIp = (isset($_SERVER['REMOTE_ADDR']) && ($_SERVER['REMOTE_ADDR'] != "")) ? $_SERVER['REMOTE_ADDR'] : "?";

    $name = (isset($_POST['$name']) && ( $_POST['$name']!= "")) ? $_POST['$name'] : ".....";

    $time = gmdate( "M d Y g:i a");

    $hostName = gethostbyaddr($userIp);

    $uri = (isset($_SERVER['REQUEST_URI']) && ($_SERVER['REQUEST_URI'] != "")) ? $_SERVER['REQUEST_URI'] : "Unknown";

    $refferer = (isset($_SERVER['HTTP_REFERER']) && ($_SERVER['HTTP_REFERER'] != "")) ? $_SERVER['HTTP_REFERER'] : "?";

    $logEntry = " $userIp $name $time $hostName $uri $refferer\n";

    if (!file_exists(LOG_FILE))
    {$logFile = fopen(LOG_FILE,"w");fwrite($logFile, $logfileHeader); }
    else
    {$logFile = fopen(LOG_FILE,"a");}

    fwrite($logFile,$logEntry);
    fclose($logFile);
    ?>
     
    keith51, Oct 12, 2007 IP
  2. kreoton

    kreoton Peon

    Messages:
    229
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Here you go :)

    <?php
    define ('BAN_LIST', 'banlist.txt');
    /*
    ips in file as following
    xxx.xxx.xxx.xxx
    xxx.xxx.xxx.xxx
    xxx.xxx.xxx.xxx
    xxx.xxx.xxx.xxx
    */
    
    define("LOG_FILE","visitors.txt");
    
    $logfileHeader=' Forum Visitors Log;
    
    IP NAME (add 1 hour) WHO IN FROM '."\n";
    
    $userIp = (isset($_SERVER['REMOTE_ADDR']) && ($_SERVER['REMOTE_ADDR'] != "")) ? $_SERVER['REMOTE_ADDR'] : "?";
    
    if (file_exists(BAN_LIST))
    {
    	$fp = fopen (BAN_LIST, 'r');
    	while (!feof($fp))
    	{
    		$line = fgets($fp, 1024);
    		if ($line == $userIp)
    		{
    			die('<h1>You are banned from using this site!</h1>'); //here script stops
    		}
    	}
    }
    
    $name = (isset($_POST['$name']) && ( $_POST['$name']!= "")) ? $_POST['$name'] : ".....";
    
    $time = gmdate( "M d Y g:i a");
    
    $hostName = gethostbyaddr($userIp);
    
    $uri = (isset($_SERVER['REQUEST_URI']) && ($_SERVER['REQUEST_URI'] != "")) ? $_SERVER['REQUEST_URI'] : "Unknown";
    
    $refferer = (isset($_SERVER['HTTP_REFERER']) && ($_SERVER['HTTP_REFERER'] != "")) ? $_SERVER['HTTP_REFERER'] : "?";
    
    $logEntry = " $userIp $name $time $hostName $uri $refferer\n";
    
    if (!file_exists(LOG_FILE))
    {$logFile = fopen(LOG_FILE,"w");fwrite($logFile, $logfileHeader); }
    else
    {$logFile = fopen(LOG_FILE,"a");}
    
    fwrite($logFile,$logEntry);
    fclose($logFile);
    ?>
    PHP:
     
    kreoton, Oct 12, 2007 IP
  3. keith51

    keith51 Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks kreoton nice to meet you but the code you added Bans the IP's ( i just tried it ) :eek:

    I was after an addition that would Not log certain IP's but still allow them access ie my IP and the bots if you see what i mean .

    Keith
     
    keith51, Oct 12, 2007 IP
  4. kreoton

    kreoton Peon

    Messages:
    229
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #4
    
    <?php
    define ('NOT_LOG', 'notloglist.txt');
    /*
    ips in file as following
    xxx.xxx.xxx.xxx
    xxx.xxx.xxx.xxx
    xxx.xxx.xxx.xxx
    xxx.xxx.xxx.xxx
    */
    
    define("LOG_FILE","visitors.txt");
    
    $log = true;
    
    $logfileHeader=' Forum Visitors Log;
    
    IP NAME (add 1 hour) WHO IN FROM '."\n";
    
    $userIp = (isset($_SERVER['REMOTE_ADDR']) && ($_SERVER['REMOTE_ADDR'] != "")) ? $_SERVER['REMOTE_ADDR'] : "?";
    
    if (file_exists(NOT_LOG))
    {
        $fp = fopen (NOT_LOG, 'r');
        while (!feof($fp))
        {
            $line = fgets($fp, 1024);
            if ($line == $userIp)
            {
                $log =false;
            }
        }
    }
    if ($log)
    {
    	$name = (isset($_POST['$name']) && ( $_POST['$name']!= "")) ? $_POST['$name'] : ".....";
    
    	$time = gmdate( "M d Y g:i a");
    
    	$hostName = gethostbyaddr($userIp);
    
    	$uri = (isset($_SERVER['REQUEST_URI']) && ($_SERVER['REQUEST_URI'] != "")) ? $_SERVER['REQUEST_URI'] : "Unknown";
    
    	$refferer = (isset($_SERVER['HTTP_REFERER']) && ($_SERVER['HTTP_REFERER'] != "")) ? $_SERVER['HTTP_REFERER'] : "?";
    
    	$logEntry = " $userIp $name $time $hostName $uri $refferer\n";
    
    	if (!file_exists(LOG_FILE))
    	{$logFile = fopen(LOG_FILE,"w");fwrite($logFile, $logfileHeader); }
    	else
    	{$logFile = fopen(LOG_FILE,"a");}
    
    	fwrite($logFile,$logEntry);
    	fclose($logFile);
    }
    ?>
    
    PHP:
     
    kreoton, Oct 12, 2007 IP
  5. keith51

    keith51 Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Many thanks kreoton it works a treat now ! and i am trying to digest the code you added :) my meager thoughts are in brackets on tight of each line (..... )

    if (file_exists(NOT_LOG)) ( checks for log file )
    {
    $fp = fopen (NOT_LOG, 'r'); ( open the file and read the Line )
    while (!feof($fp)) ( check for end of file )
    {
    $line = fgets($fp, 1024); ( is $fgets reading notloglist.txt ?? )
    if ($line == $userIp) ( is the data in file equal to $userIp
    {
    $log =false; ( if not then carry on logging IP ?


    Are my thoughts right or have i not grasped the If or vars
     
    keith51, Oct 12, 2007 IP
  6. kreoton

    kreoton Peon

    Messages:
    229
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #6
    yes your thoughts are right just:

    $line = fgets($fp, 1024); ( reads line from file and moves pointer to next line start )
     
    kreoton, Oct 12, 2007 IP
  7. keith51

    keith51 Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Phew thanks for bearing with me :) and im glad i have it nearly right as that will let me build on my mistakes , one odd thing im finding is if i enter more than one IP in the " notloglist.txt" ie
    74.6.19.234
    85.29.235.159

    It does not stop any of them .

    Is there a syntax thing i have missed when entering IP's on list :(
     
    keith51, Oct 12, 2007 IP
  8. keith51

    keith51 Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I think i will have to start again as it not even stopping just one IP ( mine ) on the list on it's own ...

    I will go away and come back later on afresh ! to see if i have missed something arghhhhhhhh :(
     
    keith51, Oct 12, 2007 IP
  9. keith51

    keith51 Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
     
    keith51, Oct 12, 2007 IP