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
by 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.
-
- function IPCheck()
- {
- if(getenv($_SERVER['HTTP_X_FORWARDED_FOR'])) {
- $IPadres = getenv($_SERVER['HTTP_X_FORWARDED_FOR']);
- }
- elseif(getenv($_SERVER['HTTP_CLIENT_IP'])) {
- $IPadres = getenv($_SERVER['HTTP_CLIENT_IP']);
- }
- else {
- $IPadres = $_SERVER['REMOTE_ADDR'];
- }
- if (! ereg ("([0-9]{1,3}\.){3}([0-9]){1,3}", $IPadres)) {
- //Own fault displayer
- die("Invalid IP Addres, access is blocked from this webpage. <br /> Invalid IP:'".$IPadres."' ");
- }
- $UserData = array();
- $UserData['IP'] = $IPadres;
- $UserData['HOST'] = gethostbyaddr($IPadres);
- return $UserData;
- }
-
example
-
- $UserData = IPCheck();
- // Or you get the error, or you can use the variable $UserData
- echo 'You're connected with: '.$UserData['IP']. ' <br />
- You're host/provider: '.$UserData['HOST']. ' <br /><hr />';
-
-
-
Rifke
- Pero pero
-
- Posts: 719
- Joined: Thu Aug 09, 2007 3:01 pm
- Location: Belgium
-
by Vetyst on Tue Jan 19, 2016 2:28 pm
PHP introduced a new function since version 5.2.
-
- mixed filter_var ( mixed $variable [, int $filter = FILTER_DEFAULT [, mixed $options ]] );
-
Using this function we can change the code above to:
-
- function IPCheck()
- {
- if (getenv($_SERVER['HTTP_X_FORWARDED_FOR'])) {
- $IpAddress = getenv($_SERVER['HTTP_X_FORWARDED_FOR']);
- } elseif (getenv($_SERVER['HTTP_CLIENT_IP'])) {
- $IpAddress = getenv($_SERVER['HTTP_CLIENT_IP']);
- } else {
- $IpAddress = $_SERVER['REMOTE_ADDR'];
- }
-
- if (filter_var($IpAddress, FILTER_VALIDATE_IP) === false) {
- die("Invalid IP Addres, access is blocked from this webpage. <br /> Invalid IP:'{$IpAddress}'");
- }
-
- return array(
- 'IP' => $IpAddress,
- 'HOST' => gethostbyaddr($IpAddress)
- );
- }
-
-
Vetyst
- osRose dev
-
- Posts: 146
- Joined: Sat Jun 18, 2011 10:56 am
- Location: The Netherlands
-
Return to PHP / Web Guides, Scripts and tools.
Users browsing this forum: No registered users and 6 guests