Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    May 2004
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Arrow Recording IP addresses with PHP??

    Here I have a PHP session counter that also records the IP of a person visiting the site in a text file:

    PHP Code:
    <?php 
    session_start
    ();    
    session_register('counter');       
    $CountFile "folder_name/counter.txt";    
    $Count file($CountFile);    
    $Count implode(""$Count);    
    if(!
    $_SESSION['counter']){    
    $counter true;    
    $OpenFile fopen($CountFile"r+");    
    $Count++;    
    $user_ip GetHostByName($_SERVER['REMOTE_ADDR']);    
    $banned = array('142.67.105.80''142.67.105.81');    
    if (!
    in_array($user_ip$banned)) {    
      if(
    $OpenFile){    
      
    fwrite($OpenFile$Count);    
      
    fclose($OpenFile);    
      }      
    }    

    // loads the ip file into an array 
    $file file("folder_name/ipaddress.txt"); 
    // checks to see if the users ip is already in the text file 
    if (!in_array($user_ip$file) { 
      
    $ip_file "folder_name/ipaddress.txt";    
      
    $ip fopen($ip_file"a+");    
      
    fwrite($ip"$user_ipn");    
      
    fclose($ip);      

    }    
    ?>
    What I want it to do is check to see if the IP address from the current session is already recorded in the text file, and if it is, don't record it again, if not record it once then start on a new line. It works well except it still records the IP address from a session more then once. HELP would be great!!

  • #2
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    why not simply check if the ip occurs with a strpos()
    the way you grab the IP also looks strange to me.
    PHP Code:
    if (isset ($_SERVER['HTTP_X_FORWARDED_FOR'])) {
       
    $user_ip $_SERVER['HTTP_X_FORWARDED_FOR'];
    } else {
       
    $user_ip $_SERVER['REMOTE_ADDR'];
    }
    $string file_get_contents('folder_name/ipaddress.txt');
    $pos strpos($string $user_ip);
    if (
    $pos === false) {
       
    // write to file

    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html


  •  

    Posting Permissions

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