Ip Checker

This is guide mostly for website tools and code, for example CMS or registration scripts.
There can also be tools.

Moderators: osRose dev team, ospRose dev team, osiRose dev team, Moderators

Ip Checker

Postby Rifke on Thu Aug 09, 2007 3:07 pm

You can find out if the user's IP address is a valid one, or an invalid one. You can expand this with an IP blocker aswel. Or a complete provider block.

  1.  
  2. function IPCheck()
  3. {
  4.     if(getenv($_SERVER['HTTP_X_FORWARDED_FOR'])) {  
  5.         $IPadres = getenv($_SERVER['HTTP_X_FORWARDED_FOR']);  
  6.     }
  7.     elseif(getenv($_SERVER['HTTP_CLIENT_IP'])) {  
  8.         $IPadres = getenv($_SERVER['HTTP_CLIENT_IP']);  
  9.     }
  10.     else {  
  11.         $IPadres = $_SERVER['REMOTE_ADDR'];  
  12.     }
  13.     if (! ereg ("([0-9]{1,3}\.){3}([0-9]){1,3}", $IPadres)) {
  14.       //Own fault displayer
  15.       die("Invalid IP Addres, access is blocked from this webpage. <br /> Invalid IP:'".$IPadres."' ");
  16.     }
  17.     $UserData = array();
  18.     $UserData['IP'] = $IPadres;
  19.     $UserData['HOST'] = gethostbyaddr($IPadres);
  20.     return $UserData;
  21. }
  22.  



example
  1.  
  2. $UserData = IPCheck();
  3. // Or you get the error, or you can use the variable $UserData
  4. echo 'You're connected with: '.$UserData['IP']. ' <br />
  5.         You're host/provider: '.$UserData['HOST']. ' <br /><hr />';
  6.  
  7.  
Rifke
Pero pero
Pero pero
 
Posts: 719
Joined: Thu Aug 09, 2007 3:01 pm
Location: Belgium

Re: Ip Checker

Postby Vetyst on Tue Jan 19, 2016 2:28 pm

PHP introduced a new function since version 5.2.

  1.  
  2. mixed filter_var ( mixed $variable [, int $filter = FILTER_DEFAULT [, mixed $options ]] );
  3.  


Using this function we can change the code above to:
  1.  
  2. function IPCheck()
  3. {
  4.     if (getenv($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  5.         $IpAddress = getenv($_SERVER['HTTP_X_FORWARDED_FOR']);
  6.     } elseif (getenv($_SERVER['HTTP_CLIENT_IP'])) {
  7.         $IpAddress = getenv($_SERVER['HTTP_CLIENT_IP']);
  8.     } else {
  9.         $IpAddress = $_SERVER['REMOTE_ADDR'];
  10.     }
  11.  
  12.     if (filter_var($IpAddress, FILTER_VALIDATE_IP) === false) {
  13.         die("Invalid IP Addres, access is blocked from this webpage. <br /> Invalid IP:'{$IpAddress}'");
  14.     }
  15.  
  16.     return array(
  17.         'IP' => $IpAddress,
  18.         'HOST' => gethostbyaddr($IpAddress)
  19.     );
  20. }
  21.  
User avatar
Vetyst
osRose dev
osRose dev
 
Posts: 142
Joined: Sat Jun 18, 2011 10:56 am
Location: The Netherlands


Return to PHP / Web Guides, Scripts and tools.

Who is online

Users browsing this forum: No registered users and 2 guests

cron