/ip2Country.class.php

https://bitbucket.org/thecoderin/ip2country · PHP · 119 lines · 79 code · 3 blank · 37 comment · 17 complexity · 66b33e28920778e7c842c1737b8eb304 MD5 · raw file

  1. <?php
  2. /**
  3. * @category ip2Country
  4. * @package ip2Country Class
  5. * @uses <APIs>
  6. * @author Anish Karim <thecoderin@gmail.com>
  7. * @license http://opensource.org/licenses/GPL-3.0
  8. */
  9. class ip2country {
  10. /**
  11. * Construct Function
  12. * To setting up the constants and making available for the scope.
  13. */
  14. function __construct() {
  15. $this->apiURL = "http://ip2country.sourceforge.net/ip2c.php?format=XML&ip="; //ip2Country API URL in format.
  16. $this->europeCountries = array("AL", "AD", "AM", "AT",
  17. "BY","BE", "BA", "BG",
  18. "CH", "CY","CZ",
  19. "DE", "DK", "EE", "ES",
  20. "FO", "FI", "FR",
  21. "GB", "GE","GI", "GR",
  22. "HU", "HR",
  23. "IE","IS", "IT",
  24. "LT", "LU", "LV",
  25. "MC", "MK", "MT",
  26. "NO", "NL",
  27. "PO", "PT",
  28. "RO", "RU",
  29. "SE","SI", "SK", "SM",
  30. "TR", "UA", "VA", "EU"); //Europe Country List
  31. $this->asianCountries = array("AF", "CN", "VN", "TH", "CN","AZ", "BH", "BD", "BT", "BN","KH", "CY", "GE", "IN", "ID","IR", "IQ", "IL", "JP", "JO","KZ", "KP", "KR", "KW", "KG","LA", "LB", "MY", "MV", "MN","MM", "NP", "OM", "PK", "PH","QA", "RU", "SA", "SG", "LK","SY", "TJ", "TH", "TR", "TM","AE", "UZ", "YE", "HK", "TW");//Asia Pacefic Country List
  32. /* Add additional Region here */
  33. }
  34. /**
  35. * Funtion getRegion
  36. * scope Public
  37. * @param none.
  38. * @return specified region (string).
  39. */
  40. public function getRegion() {
  41. $region = 'No Region Assigned';//setting up the default region No Region, if this happens please add new region/country to the class.
  42. $myIp = $this->getIpAddress();
  43. $URL = $this->apiURL . $myIp;
  44. $data = file_get_contents($URL); //receiving response from the API URL.
  45. $xml = new SimpleXMLElement($data);
  46. if ($xml->country_code) {
  47. $countryCode = $xml->country_code;
  48. /**
  49. * Add Region or country checks in below switch-case block.
  50. * If it is a country add a case block
  51. * If its a region add the region array in constructor and add the condition in default block.
  52. */
  53. switch ($countryCode) {
  54. case 'US':
  55. $region = 'United States';
  56. break;
  57. case 'CA':
  58. $region = 'Canada';
  59. break;
  60. default:
  61. if (in_array($countryCode, $this->europeCountries))
  62. $region = 'Europe';
  63. if (in_array($countryCode, $this->asianCountries))
  64. $region = 'Asia Pacefic';
  65. break;
  66. }
  67. }
  68. return $region;
  69. }
  70. /**
  71. * Function getIpAddress()
  72. * scope Protected
  73. * @param none
  74. * @return public Ip Address(string)
  75. * This function will check several possibilities and return the client's public IP.
  76. */
  77. protected function getIpAddress() {
  78. if($this->validateIp(getenv("HTTP_CLIENT_IP"))) return getenv("HTTP_CLIENT_IP");
  79. if ($this->validateIp(getenv("HTTP_X_FORWARDED"))) return getenv("HTTP_X_FORWARDED");
  80. foreach (explode(",",getenv("HTTP_X_FORWARDED_FOR")) as $ip) {
  81. if ($this->validateIp($ip)) return $ip;
  82. }
  83. if ($this->validateIp(getenv("HTTP_X_CLUSTER_CLIENT_IP"))) return getenv("HTTP_X_CLUSTER_CLIENT_IP");
  84. if ($this->validateIp(getenv("HTTP_FORWARDED_FOR"))) return getenv("HTTP_FORWARDED_FOR");
  85. if ($this->validateIp(getenv("HTTP_FORWARDED"))) return getenv("HTTP_FORWARDED");
  86. if ($this->validateIp(getenv("REMOTE_ADDR"))) return getenv("REMOTE_ADDR");
  87. $fetchedIp = file_get_contents("http://www.ipmango.com/api/myip"); //get real IP through ipMango API.
  88. if ($this->validateIp($fetchedIp)) return $fetchedIp;
  89. return false;
  90. }
  91. /**
  92. * scope Protected
  93. * @param ipAddress (string)
  94. * @return true of false (boolean)
  95. * recieves the ip and put through various checks and if recieved parameter goes through all, returns true indicating its the valid ip.
  96. */
  97. protected function validateIp($ipAddress) {
  98. if (empty($ipAddress) || ip2long($ipAddress)==-1 || ip2long($ipAddress)==false) return false;
  99. $privateIpArray = array(
  100. array('min'=>'0.0.0.0','max'=>'2.255.255.255'),
  101. array('min'=>'10.0.0.0','max'=>'10.255.255.255'),
  102. array('min'=>'127.0.0.0','max'=>'127.255.255.255'),
  103. array('min'=>'169.254.0.0','max'=>'169.254.255.255'),
  104. array('min'=>'172.16.0.0','max'=>'172.31.255.255'),
  105. array('min'=>'192.0.2.0','max'=>'192.0.2.255'),
  106. array('min'=>'192.168.0.0','max'=>'192.168.255.255'),
  107. array('min'=>'255.255.255.0','max'=>'255.255.255.255')
  108. );
  109. foreach($privateIpArray as $pvtIp) {
  110. if((ip2long($ipAddress)>=ip2long($pvtIp['min'])) && (ip2long($ipAddress) <=ip2long($pvtIp['max']))) return false;
  111. }
  112. return true;
  113. }
  114. #code
  115. }
  116. ?>