IP Address Check and Compare

Discussion in 'PHP' started by lordadel, Oct 9, 2007.

  1. #1
    Hello

    i want to make a php file for a download page so the same IP address cannot download the file twice

    I want to make a php file that can check the IP of the visitor and if the IP already exists in a .txt file then it doesnt store it and if the IP doesnt exist then it stores it in the .txt file and then redirects the visitor to the download page

    and i want the maximum IPs in the .txt file to be 4000 IPs so when it reaches 4000 IP's and a new visitor come the first IP gets deleted and the new IP is stored

    i would appreciate if someone helps me

    Cheers
    Adel
     
    lordadel, Oct 9, 2007 IP
  2. kkrizka

    kkrizka Peon

    Messages:
    223
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    It's quite an easy task for a PHP programmer. I'll quickly cook up a checking script. :)
     
    kkrizka, Oct 9, 2007 IP
  3. lordadel

    lordadel Active Member

    Messages:
    1,035
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    90
    #3
    thank you mate i am waiting :)
     
    lordadel, Oct 9, 2007 IP
  4. rafiqasad

    rafiqasad Member

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #4
    Asslam-o-alikam. My this code solve your problem
    
    	$ip = trim($_SERVER['REMOTE_ADDR']);
    	
    	$myFile = "file/testFile.txt";	
    	$fh = fopen($myFile, 'a+')or die ("fail to read the file");
    	$theData = fread($fh, filesize($myFile));
    	$theData = trim($theData);
    	//echo $theData;	
    	$savefile = explode(" ",$theData);
    	
    	foreach ($savefile as $value) {
    	//print $value." - ". $ip."<br />"; 
        	if($value == $ip){		
    			$flag = 1;
    			break;
    		}
    		//echo $value."<br />";
    	}
    	if(isset($flag)){
    		echo "Ip found";//now u can do what ever u want
    		
    	}else{
    		$count = count($savefile); 
    		if($count >4000){ // total number of record		
    		$savefile[] = $ip; 
    		$savefile = array_slice($savefile,1,$count);
    		//print_r($savefile);
    		$str = implode(" ",$savefile);
    		$str = trim($str);
    		
    		$temp = "file/temp.txt";	
    		$new = fopen($temp, 'w') or die("can't open file");		
    		fwrite($new, $str);	
    		unlink("file/testFile.txt");
    		rename("file/temp.txt","file/testFile.txt");		
    		}else{
    			$ip = " ".$ip; 
    			fwrite($fh, $ip);
    		}		
    		//echo "Ip not found";
    	}
    	
    	
    	fclose($fh);
    
    
    PHP:
     
    rafiqasad, Oct 10, 2007 IP
    lordadel likes this.
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    rafiqasad beat me to it while I was coding... but I'll still post my code cause I feel like it. :p


    
    function verify_ip($log = 'ips.txt')
    {
    	$ips = array_map('trim', @file($log));
    	
    	// IP exists in file
    	if (in_array($_SERVER['REMOTE_ADDR'], $ips))
    	{
    		return false;
    	}
    	
    	$ips[] = $_SERVER['REMOTE_ADDR'];
    	
    	// Only the latest 4000 IPs
    	if (sizeof($ips) > 4000)
    	{
    		$ips = array_slice($ips, -4000);
    	}
    	
    	// Write data to file
    	if ($fp = @fopen($log, 'w'))
    	{
    		flock($fp, LOCK_EX);
    		fwrite($fp, implode("\n", $ips));
    		flock($fp, LOCK_UN);
    		fclose($fp);
    	}
    	
    	return true;
    }
    
    PHP:
    
    if (verify_ip())
    {
    	echo 'I can download';
    }
    else
    {
    	echo 'I can not download';
    }
    
    PHP:
     
    nico_swd, Oct 10, 2007 IP
    lordadel likes this.
  6. lordadel

    lordadel Active Member

    Messages:
    1,035
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    90
    #6
    thank you guys it really helped me gave + rep to you both :D
     
    lordadel, Oct 10, 2007 IP