Results 1 to 3 of 3

Thread: IP address check and compare

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    111

    Exclamation IP address check and compare

    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

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    111

    Re: IP address check and compare

    thanks guys i found it out here it is

    Code:
    $ip = trim($_SERVER['REMOTE_ADDR']);
    	
    	$myFile = "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 = "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);

  3. #3
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: IP address check and compare

    please set the thread to resolved if you have found a solution
    My usual boring signature: Something

Posting Permissions

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



Click Here to Expand Forum to Full Width