Advertisement
Guest User

IP by groups

a guest
Nov 27th, 2014
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.33 KB | None | 0 0
  1. /**
  2.  * Checking IP's or parts of an IP for session validation (for example)
  3.  *
  4.  * get_ip_groups(3) = 123.456.798
  5.  * get_ip_groups(2) = 123.456       (default)
  6.  */
  7.  
  8. /*
  9. function get_real_ip()
  10. {
  11.     $ip = null;
  12.     if(isset($_SERVER['HTTP_CF_CONNECTING_IP']))
  13.     {
  14.         $ip = $_SERVER['HTTP_CF_CONNECTING_IP']; // Get IP from Cloudflare
  15.     }
  16.     elseif (isset($_SERVER['HTTP_CLIENT_IP']))
  17.     {
  18.         $ip = $_SERVER['HTTP_CLIENT_IP']; // Get IP from share internet
  19.     }
  20.     elseif(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
  21.     {
  22.         $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; // Get IP from proxy
  23.     }
  24.     elseif(isset($_SERVER['HTTP_X_FORWARDED']))
  25.     {
  26.         $ip = $_SERVER['HTTP_X_FORWARDED']; // Get IP from proxy
  27.     }
  28.     elseif(isset($_SERVER['HTTP_FORWARDED_FOR']))
  29.     {
  30.         $ip = $_SERVER['HTTP_FORWARDED_FOR']; // Get IP from proxy
  31.     }
  32.     elseif(isset($_SERVER['HTTP_FORWARDED']))
  33.     {
  34.         $ip = $_SERVER['HTTP_FORWARDED']; // Get IP from proxy
  35.     }
  36.     elseif (isset($_SERVER['REMOTE_ADDR']))
  37.     {
  38.         $ip = $_SERVER['REMOTE_ADDR'];
  39.     }
  40.     return $ip;
  41. }
  42. */
  43.  
  44. function get_ip_groups($group = null)
  45. {
  46.     // Looking for (real ip or) ip,
  47.     $ip_addr = (function_exists('get_real_ip')) ? get_real_ip() : $_SERVER['REMOTE_ADDR'];
  48.  
  49.     $group = (is_int($group) == null || $group >= 5 || $group == 0) ? 2 : (int) $group;
  50.     $ip2check = array_slice(explode('.', $ip_addr), 0, $group);
  51.     return implode('.', $ip2check);
  52. }
  53.  
  54. ?>
  55. <p>Checking IP by {1..4} groups...<br />
  56. <em>(Default is "2", and max is "4")</em></p>
  57. <pre><code>
  58. [Examples]
  59.  
  60.     get_ip_groups(7) = <?= get_ip_groups(7) . "\n"; ?>
  61.     get_ip_groups(6) = <?= get_ip_groups(6) . "\n"; ?>
  62.     get_ip_groups(5) = <?= get_ip_groups(5); ?><strong>
  63.     get_ip_groups(4) = <?= get_ip_groups(4) . "\n"; ?>
  64.     get_ip_groups(3) = <?= get_ip_groups(3) . "\n"; ?>
  65.     get_ip_groups(2) = <?= get_ip_groups(2) . "\n"; ?>
  66.     get_ip_groups(1) = <?= get_ip_groups(1); ?></strong>
  67.     get_ip_groups(0) = <?= get_ip_groups(0) . "\n"; ?>
  68. </code></pre>
  69.  
  70. <?php
  71. $show_code = highlight_string('
  72. <?php
  73.  
  74. function get_ip_groups($group = null)
  75. {
  76.     // Looking for (real ip or) ip,
  77.     $ip_addr = (function_exists(\'get_real_ip\')) ? get_real_ip() : $_SERVER[\'REMOTE_ADDR\'];
  78.  
  79.     $group = (is_int($group) == null || $group >= 5 || $group == 0) ? 2 : (int) $group;
  80.     $ip2check = array_slice(explode('.', $ip_addr), 0, $group);
  81.     return implode('.', $ip2check);
  82. };
  83. ', true);
  84.  
  85. //echo $show_code;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement