SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Mar 2008
    Location
    Sterling, CO
    Posts
    275
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Detect if User Lives in the Continuous United States with PHP

    Hi,

    I've been searching Google to see how I could detect if someone living outside of the US. I would like to not display certain html if the website viewer lives outside the US. Specifically the contiguous US if that is possible.

    I found something like this that might work but there might be an easier way:

    PHP Code:
    <?php
     
    /*
     * ip__allocation.php
     *
     */
     
        
    $MyClient=FALSE;
        if (isset(
    $_SERVER['REMOTE_ADDR']))
        {
            include 
    'ip_in_range.php';
            
    $clientIP=$_SERVER["REMOTE_ADDR"];
            
    $IPAllocation = array(
                
    '192.168.1.0-192.168.1.255',
                
    '192.168.2.0-192.168.2.255',
            );
     
                foreach (
    $IPAllocation as $addressRange) {
     
                    if (
    ip_in_range($clientIP$addressRange))
                    {
                        
    $MyClient=TRUE;
                        break;
                    }
                }
        }
    ?>
    Thanks

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    Go grab the free versions of any of the GeoIP rendering services state CSV, such as MaxMind's.

    My suggestion then is to load this data into a database (which should be fairly easy), and then simply ip2long the IP address, and use it for a BETWEEN check. That way the database handles all of the looping/indexing that needs to be done (as this can be a rather large amount of data!) and returns only the result you're interested in (which should be either 1 row or none)
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    SitePoint Addict
    Join Date
    Mar 2008
    Location
    Sterling, CO
    Posts
    275
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. It's just a simple thing I'm wanting to do. I have free shipping but only to US customers and if the customers order is greater than then it displays "You get free shipping". I just want to not show this at all to anyone out side of the US. Creating a whole database and another account and everything for this one thing might be a little overkill but it sounds like it would work thou.


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
  •