SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Mar 2001
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Which Country are you from?

    Hiya guys,

    thanks for all the advice given to me last time, i finally fixed the problem. But i have another question, how can i get the users country that he/she is from, it can be done in perl but i don't know how to do it in php.

    Any advice is appreciated.

    Thanks

  2. #2
    SitePoint Evangelist ucahg's Avatar
    Join Date
    Apr 2001
    Location
    Sarnia, Ontario, Canada
    Posts
    434
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <HTML>
    <HEAD>
    <TITLE>
    <?PHP echo $title?>
    </TITLE>
    </HEAD>
    <BODY>
    <?PHP
    // which_country.php
    //
    //  determines what country a user is from
    //  and inputs it into a database
    mysql_connect('localhost');
    if (IsSet(
    $Submit) {
     
    //  if form was submitted, insert into database
     
    mysql_query("INSERT INTO users.country SET ip='$HTTP_USERAGENT', country='$country';");
     
    $title "Success!"
     
    echo "Country added to database";
    }
    else {
     
    // if form was not submitted, display form
     
    $title "Where ya from?";
    ?>
    <form action="<?PHP echo $PHP_SELF?>" method="post">
    <select name="country">
    <option value="Canada">Canada</option>
    <option value="United States">United States</option>
    <option value="Mexico">Mexico</option>
    </select>
    <input type="submit" name="submit" value="Add me to the database!">
    </form>
    <?PHP
    }
    ?>
    Then on the page you want to call the country:

    PHP Code:
    <?PHP
    mysql_connect
    ('localhost');
    $country mysql_query("SELECT country FROM users.country WHERE ip='$HTTP_USERAGENT';");
    echo 
    $country;
    ?>
    Last edited by ucahg; Jun 22, 2001 at 07:54.

  3. #3
    SitePoint Member
    Join Date
    Mar 2001
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry, i should have been more clearer, i want to detect the country automatically, i think its done from the users hostname in the perl script i have, it can be seen at http://hacks.xmbforum.com/cgi-bin/stat.pl

    thanks again

  4. #4
    will code HTML for food Michel V's Avatar
    Join Date
    Sep 2000
    Location
    Corsica
    Posts
    552
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Stats are done with the TLD of the visitors' ISP. So for example if you got .jp, the stats program will say you got a visitor from Japan.
    But there's an annoying thing: all .com is considered as USA, which is really stupid, since there are .com's everywhere in the world. Also, IPs are not recognized.

    So, you can do that by explode()ing the visitor's hostname, but that's not really accurate.
    [blogger: zengun] [blogware contributor: wordpress]


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •