Jump to content


New Members
  • Posts

  • Joined

  • Last visited


Profile Information

  • Gender
    Not Telling

markfiend's Achievements


Newbie (1/5)



  1. [!--quoteo(post=379105:date=Jun 1 2006, 11:02 AM:name=gnuffo1)--][div class=\'quotetop\']QUOTE(gnuffo1 @ Jun 1 2006, 11:02 AM) [snapback]379105[/snapback][/div][div class=\'quotemain\'][!--quotec--] This site [a href=\"http://api.hostip.info/?ip=\" target=\"_blank\"]http://api.hostip.info/?ip=[/a][ip address] produces an xml document giving details about the IP address, including the country. So it would probably be quite easy to write a function that gets the country code from there based on $_SERVER['REMOTE_ADDR'] [/quote] Great, fantastic, that's what I was looking for. And indeed the function is pretty easy... [code]function ipToCountry($ip='') {    if ($ip=='') $ip = $_SERVER['REMOTE_ADDR'];          $file = fopen (("http://api.hostip.info/?ip=" . $ip), "r");     if (!$file)     {          echo "<p>Unable to open remote file.\n";          exit;     }     while (!feof ($file))     {          $line = fgets ($file, 1024);          if (eregi ("<countryName>(.*)</countryName>", $line, $out))          {              $country = $out[1];              return $country;          }     }         fclose($file); }[/code] The function ipToCountry() returns "UNITED KINGDOM" for me here, and ipToCountry('') returns "AUSTRALIA" (just a random example). Thanks very much. [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /]
  2. At the moment I have this snippet on my index page: [code]if (isset($_SESSION['country'])) {     if ($_SESSION['country'] == "UK")     {         header("Location: UK_index.php");     }     else     {         header("Location: restofworld_index.php");     } } else {     header("Location: set_country.php"); }[/code] so the browser is pointed to the relevant page if the session 'country' variable is set, and a page to set it if not. That all works fine and dandy, but I'd rather people weren't diverted to a "please choose your country" page when they first arrive at the site; I think it could put visitors off. I found the [a href=\"http://www.phpfreaks.com/quickcode/IP-logger/624.php\" target=\"_blank\"]IP Logger[/a] in the code library but I've tried it and it returns my country as "com" even though I'm in the UK [img src=\"style_emoticons/[#EMO_DIR#]/huh.gif\" style=\"vertical-align:middle\" emoid=\":huh:\" border=\"0\" alt=\"huh.gif\" /] So is there any other way for php to tell me from what country someone is browsing to my site? Thanks in advance folks.
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.