Ver Mensaje Individual
  #38 (permalink)  
Antiguo 18/07/2015, 22:58
Avatar de chronos682
chronos682
 
Fecha de Ingreso: febrero-2004
Ubicación: Tunja - Boyacá - Colombia
Mensajes: 627
Antigüedad: 20 años, 3 meses
Puntos: 69
Respuesta: Codigo Formulario de envio con pais

Hay un problema con el código que dije funcionaba y es que en un servidor local funciona bien pero al subirlo al servidor web no lo deja correr por el tamaño del archivo php y en especial por el tamaño del array que supera el lìmite impuesto por el servidor, lo probé en 2 servidores y en ninguno corrió, por lo cual estuve buscando otra forma de hacerlo y buscando aquí y allá pude juntar dos códigos fuente y armar esta función porque ninguna de las dos funcionaba por si sola ya. Espero les sirva si alguno quiere intentarlo:

Código PHP:
Ver original
  1. <?php
  2. function GetNameCtry($code)
  3. {
  4.     $paises = array(
  5.         "AD"=>"Andorra",
  6.         "AE"=>"United Arab Emirates",
  7.         "AF"=>"Afghanistan",
  8.         "AG"=>"Antigua & Barbuda",
  9.         "AI"=>"Anguilla",
  10.         "AL"=>"Albania",
  11.         "AM"=>"Armenia",
  12.         "AN"=>"Netherlands Antilles",
  13.         "AO"=>"Angola",
  14.         "AQ"=>"Antarctica",
  15.         "AR"=>"Argentina",
  16.         "AS"=>"American Samoa",
  17.         "AT"=>"Austria",
  18.         "AU"=>"Australia",
  19.         "AW"=>"Aruba",
  20.         "AZ"=>"Azerbaijan",
  21.         "BA"=>"Bosnia and Herzegovina",
  22.         "BB"=>"Barbados",
  23.         "BD"=>"Bangladesh",
  24.         "BE"=>"Belgium",
  25.         "BF"=>"Burkina Faso",
  26.         "BG"=>"Bulgaria",
  27.         "BH"=>"Bahrain",
  28.         "BI"=>"Burundi",
  29.         "BJ"=>"Benin",
  30.         "BM"=>"Bermuda",
  31.         "BN"=>"Brunei Darussalam",
  32.         "BO"=>"Bolivia",
  33.         "BR"=>"Brazil",
  34.         "BS"=>"Bahama",
  35.         "BT"=>"Bhutan",
  36.         "BU"=>"Burma (no longer exists)",
  37.         "BV"=>"Bouvet Island",
  38.         "BW"=>"Botswana",
  39.         "BY"=>"Belarus",
  40.         "BZ"=>"Belize",
  41.         "CA"=>"Canada",
  42.         "CC"=>"Cocos (Keeling) Islands",
  43.         "CF"=>"Central African Republic",
  44.         "CG"=>"Congo",
  45.         "CH"=>"Switzerland",
  46.         "CI"=>"Côte D'ivoire (Ivory Coast)",
  47.         "CK"=>"Cook Iislands",
  48.         "CL"=>"Chile",
  49.         "CM"=>"Cameroon",
  50.         "CN"=>"China",
  51.         "CO"=>"Colombia",
  52.         "CR"=>"Costa Rica",
  53.         "CS"=>"Czechoslovakia (no longer exists)",
  54.         "CU"=>"Cuba",
  55.         "CV"=>"Cape Verde",
  56.         "CX"=>"Christmas Island",
  57.         "CY"=>"Cyprus",
  58.         "CZ"=>"Czech Republic",
  59.         "DD"=>"German Democratic Republic (no longer exists)",
  60.         "DE"=>"Germany",
  61.         "DJ"=>"Djibouti",
  62.         "DK"=>"Denmark",
  63.         "DM"=>"Dominica",
  64.         "DO"=>"Dominican Republic",
  65.         "DZ"=>"Algeria",
  66.         "EC"=>"Ecuador",
  67.         "EE"=>"Estonia",
  68.         "EG"=>"Egypt",
  69.         "EH"=>"Western Sahara",
  70.         "ER"=>"Eritrea",
  71.         "ES"=>"Spain",
  72.         "ET"=>"Ethiopia",
  73.         "FI"=>"Finland",
  74.         "FJ"=>"Fiji",
  75.         "FK"=>"Falkland Islands (Malvinas)",
  76.         "FM"=>"Micronesia",
  77.         "FO"=>"Faroe Islands",
  78.         "FR"=>"France",
  79.         "FX"=>"France, Metropolitan",
  80.         "GA"=>"Gabon",
  81.         "GB"=>"United Kingdom (Great Britain)",
  82.         "GD"=>"Grenada",
  83.         "GE"=>"Georgia",
  84.         "GF"=>"French Guiana",
  85.         "GH"=>"Ghana",
  86.         "GI"=>"Gibraltar",
  87.         "GL"=>"Greenland",
  88.         "GM"=>"Gambia",
  89.         "GN"=>"Guinea",
  90.         "GP"=>"Guadeloupe",
  91.         "GQ"=>"Equatorial Guinea",
  92.         "GR"=>"Greece",
  93.         "GS"=>"South Georgia and the South Sandwich Islands",
  94.         "GT"=>"Guatemala",
  95.         "GU"=>"Guam",
  96.         "GW"=>"Guinea-Bissau",
  97.         "GY"=>"Guyana",
  98.         "HK"=>"Hong Kong",
  99.         "HM"=>"Heard & McDonald Islands",
  100.         "HN"=>"Honduras",
  101.         "HR"=>"Croatia",
  102.         "HT"=>"Haiti",
  103.         "HU"=>"Hungary",
  104.         "ID"=>"Indonesia",
  105.         "IE"=>"Ireland",
  106.         "IL"=>"Israel",
  107.         "IN"=>"India",
  108.         "IO"=>"British Indian Ocean Territory",
  109.         "IQ"=>"Iraq",
  110.         "IR"=>"Islamic Republic of Iran",
  111.         "IS"=>"Iceland",
  112.         "IT"=>"Italy",
  113.         "JM"=>"Jamaica",
  114.         "JO"=>"Jordan",
  115.         "JP"=>"Japan",
  116.         "KE"=>"Kenya",
  117.         "KG"=>"Kyrgyzstan",
  118.         "KH"=>"Cambodia",
  119.         "KI"=>"Kiribati",
  120.         "KM"=>"Comoros",
  121.         "KN"=>"St. Kitts and Nevis",
  122.         "KP"=>"Korea, Democratic People's Republic of",
  123.         "KR"=>"Korea, Republic of",
  124.         "KW"=>"Kuwait",
  125.         "KY"=>"Cayman Islands",
  126.         "KZ"=>"Kazakhstan",
  127.         "LA"=>"Lao People's Democratic Republic",
  128.         "LB"=>"Lebanon",
  129.         "LC"=>"Saint Lucia",
  130.         "LI"=>"Liechtenstein",
  131.         "LK"=>"Sri Lanka",
  132.         "LR"=>"Liberia",
  133.         "LS"=>"Lesotho",
  134.         "LT"=>"Lithuania",
  135.         "LU"=>"Luxembourg",
  136.         "LV"=>"Latvia",
  137.         "LY"=>"Libyan Arab Jamahiriya",
  138.         "MA"=>"Morocco",
  139.         "MC"=>"Monaco",
  140.         "MD"=>"Moldova, Republic of",
  141.         "MG"=>"Madagascar",
  142.         "MH"=>"Marshall Islands",
  143.         "ML"=>"Mali",
  144.         "MN"=>"Mongolia",
  145.         "MM"=>"Myanmar",
  146.         "MO"=>"Macau",
  147.         "MP"=>"Northern Mariana Islands",
  148.         "MQ"=>"Martinique",
  149.         "MR"=>"Mauritania",
  150.         "MS"=>"Monserrat",
  151.         "MT"=>"Malta",
  152.         "MU"=>"Mauritius",
  153.         "MV"=>"Maldives",
  154.         "MW"=>"Malawi",
  155.         "MX"=>"Mexico",
  156.         "MY"=>"Malaysia",
  157.         "MZ"=>"Mozambique",
  158.         "NA"=>"Namibia",
  159.         "NC"=>"New Caledonia",
  160.         "NE"=>"Niger",
  161.         "NF"=>"Norfolk Island",
  162.         "NG"=>"Nigeria",
  163.         "NI"=>"Nicaragua",
  164.         "NL"=>"Netherlands",
  165.         "NO"=>"Norway",
  166.         "NP"=>"Nepal",
  167.         "NR"=>"Nauru",
  168.         "NT"=>"Neutral Zone (no longer exists)",
  169.         "NU"=>"Niue",
  170.         "NZ"=>"New Zealand",
  171.         "OM"=>"Oman",
  172.         "PA"=>"Panama",
  173.         "PE"=>"Peru",
  174.         "PF"=>"French Polynesia",
  175.         "PG"=>"Papua New Guinea",
  176.         "PH"=>"Philippines",
  177.         "PK"=>"Pakistan",
  178.         "PL"=>"Poland",
  179.         "PM"=>"St. Pierre & Miquelon",
  180.         "PN"=>"Pitcairn",
  181.         "PR"=>"Puerto Rico",
  182.         "PT"=>"Portugal",
  183.         "PW"=>"Palau",
  184.         "PY"=>"Paraguay",
  185.         "QA"=>"Qatar",
  186.         "RE"=>"Réunion",
  187.         "RO"=>"Romania",
  188.         "RU"=>"Russian Federation",
  189.         "RW"=>"Rwanda",
  190.         "SA"=>"Saudi Arabia",
  191.         "SB"=>"Solomon Islands",
  192.         "SC"=>"Seychelles",
  193.         "SD"=>"Sudan",
  194.         "SE"=>"Sweden",
  195.         "SG"=>"Singapore",
  196.         "SH"=>"St. Helena",
  197.         "SI"=>"Slovenia",
  198.         "SJ"=>"Svalbard & Jan Mayen Islands",
  199.         "SK"=>"Slovakia",
  200.         "SL"=>"Sierra Leone",
  201.         "SM"=>"San Marino",
  202.         "SN"=>"Senegal",
  203.         "SO"=>"Somalia",
  204.         "SR"=>"Suriname",
  205.         "ST"=>"Sao Tome & Principe",
  206.         "SU"=>"Union of Soviet Socialist Republics (no longer exists)",
  207.         "SV"=>"El Salvador",
  208.         "SY"=>"Syrian Arab Republic",
  209.         "SZ"=>"Swaziland",
  210.         "TC"=>"Turks & Caicos Islands",
  211.         "TD"=>"Chad",
  212.         "TF"=>"French Southern Territories",
  213.         "TG"=>"Togo",
  214.         "TH"=>"Thailand",
  215.         "TJ"=>"Tajikistan",
  216.         "TK"=>"Tokelau",
  217.         "TM"=>"Turkmenistan",
  218.         "TN"=>"Tunisia",
  219.         "TO"=>"Tonga",
  220.         "TP"=>"East Timor",
  221.         "TR"=>"Turkey",
  222.         "TT"=>"Trinidad & Tobago",
  223.         "TV"=>"Tuvalu",
  224.         "TW"=>"Taiwan, Province of China",
  225.         "TZ"=>"Tanzania, United Republic of",
  226.         "UA"=>"Ukraine",
  227.         "UG"=>"Uganda",
  228.         "UM"=>"United States Minor Outlying Islands",
  229.         "US"=>"United States of America",
  230.         "UY"=>"Uruguay",
  231.         "UZ"=>"Uzbekistan",
  232.         "VA"=>"Vatican City State (Holy See)",
  233.         "VC"=>"St. Vincent & the Grenadines",
  234.         "VE"=>"Venezuela",
  235.         "VG"=>"British Virgin Islands",
  236.         "VI"=>"United States Virgin Islands",
  237.         "VN"=>"Viet Nam",
  238.         "VU"=>"Vanuatu",
  239.         "WF"=>"Wallis & Futuna Islands",
  240.         "WS"=>"Samoa",
  241.         "YD"=>"Democratic Yemen (no longer exists)",
  242.         "YE"=>"Yemen",
  243.         "YT"=>"Mayotte",
  244.         "YU"=>"Yugoslavia",
  245.         "ZA"=>"South Africa",
  246.         "ZM"=>"Zambia",
  247.         "ZR"=>"Zaire",
  248.         "ZW"=>"Zimbabwe",
  249.         "ZZ"=>"Unknown or unspecified country",
  250.     );
  251.     return $paises[$code]; 
  252. }
  253. function getCountry($ip_address){
  254.     //By Marc Palau (http://www.nbsp.es)
  255.     $url = "http://ipinfodb.com/ip_locator.php";
  256.     $ch = curl_init();
  257.     curl_setopt($ch, CURLOPT_URL, $url);
  258.     curl_setopt($ch, CURLOPT_POST,"POST");
  259.     curl_setopt($ch, CURLOPT_POST, 1);
  260.     curl_setopt($ch, CURLOPT_POSTFIELDS, "ip=$ip_address");
  261.  
  262.     ob_start();
  263.  
  264.     curl_exec($ch);
  265.     curl_close($ch);
  266.     $html = ob_get_contents();
  267.     ob_end_clean();
  268.  
  269.     preg_match("/<li>Country : (.*?) <img/",$html,$data);
  270.     $d['pais'] = $data[1];
  271.     preg_match("/<li>State\/Province : (.*?)<\/li>/",$html,$data);
  272.     $d['state'] = $data[1];
  273.     preg_match("/<li>City : (.*?)<\/li>/",$html,$data);
  274.     $d['city'] = $data[1];
  275.     return ($d);
  276. }
  277. function getRealIP() {
  278.     if (!empty($_SERVER['HTTP_CLIENT_IP']))
  279.         return $_SERVER['HTTP_CLIENT_IP'];
  280.  
  281.     if (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
  282.         return $_SERVER['HTTP_X_FORWARDED_FOR'];
  283.  
  284.     return $_SERVER['REMOTE_ADDR'];
  285. }
  286. $ip = getRealIP();
  287. $iploc = getCountry($ip);
  288. $namepais = GetNameCtry($iploc["pais"]);
  289. echo $iploc["city"].", ".$iploc["state"].", ".$namepais." (".$iploc["pais"].")";
  290. ?>

El código se basa en interactuar con una página que ya hace el "trabajo sucio" que es determinar la ubicación de la IP y luego a partir del código fuente extraer la cadena que se necesita.

PD: Por si les interesa también, en la página desde donde se extrae la información hay la posibilidad de registrarse (al parecer gratis) y utilizar una API que ponen a disposición y la cual genera un archivo XML con la información del país correspondiente a la IP, la dirección es esta:

IP Location API
__________________
Si te gustó la respuesta dale +1

HERNÁN G. SIABATO M.
[email protected]

Última edición por chronos682; 19/07/2015 a las 00:28