Rich
Rich

Reputation: 512

Looking up a location from geoio.com, then acting on the user's country

ok I'm using geoio.com to find out a users location, I get back a load of info based on this bit of code

<?php $_SESSION['geo'] = file_get_contents("http://api.geoio.com/q.php?key=********=geoip&d=comma&q=" . getenv('REMOTE_ADDR') . ""); ?>

The above set's the session variable to

Chesterfield,Derbyshire,United Kingdom,Greenfrog Computing Ltd Managed Broadband Adsl Ass,53.2500,-1.4167,GB)

So all I want to do, is if that session variable contains United States , is redirect users to a different URL called /us/

How do I 'find' the United States bit and act on it.

Any help greatly appreciated ! (note I edited out the API key)

Cheers

Rich :)

Upvotes: 0

Views: 75

Answers (2)

web-nomad
web-nomad

Reputation: 6003

Try this:

Assume your session variable is location.

<?php
$_SESSION['location'] = 'Chesterfield,Derbyshire,`United States`,Greenfrog Computing Ltd Managed Broadband Adsl Ass,53.2500,-1.4167,GB';

$tmp = explode( ',', $_SESSION['location'] );

$string_to_search = 'United States'
$key = array_search( $string_to_search, $tmp );

if($key !== false) {
     // go to `us` folder
}
?>

Hope it helps.

Upvotes: 0

MrCode
MrCode

Reputation: 64526

Use strpos() which returns false if the string was not found, or the index if it was.

if(strpos($_SESSION['geo'], 'United States') !== false)
{
    header('Location: /us/');
    exit();
}

Upvotes: 1

Related Questions