Log in

View Full Version : web content depending on country?



devindevil
10-07-2006, 02:12 AM
Hi I was wondering if anyone knew of a code or how to have different content depending on the country of the visitor to your site. For example I have seen on several sites, that once your IP is detected you're directed to content only available to the country from which you are visiting from. Does anyone have any idea how to do this? Is it possible with html?

An answer would be appreciated!

thanks

-devin

djr33
10-07-2006, 08:48 AM
1. NOT possible with just html.
2. Javascript *might* work, but wouldn't be very compatible or trustworthy. It would also be quite hard to code, for a number of reasons.
3. PHP (or another similar serverside programming language) is what you want.

However... the IP doesn't say "USA"... it says an almost random series of numbers.

I have no idea how they're distributed and I don't think it's an obvious setup, like everything from 1-3 for the first number is from USA, etc.
However, there is some way of knowing it.
The problem is that you need to know this. Handcoding it into the code would be a nightmare.
Therefore, the only logical way to do it is to connect to a database that stores the info.
Maybe google it and see if you can find out.
If not, then you likely won't be able to do it.
Notice that the companies doing this have a large budget to support it ;)

One possible suggestion would be to use php to take the IP, send it to a service that traces the IP and gives/suggests a country, then use that in the php code.
However, note that some of the sites you can track IPs with say very clearly that it is ILLEGAL to perform operations on their site automatically... so be careful.

If/once you have the country, you can use php to redirect to the right site, or set variables. Depends on what you want, exactly.

devindevil
10-07-2006, 08:10 PM
Thanks for the reply djr33!

It sounds more dificult than I imagined, and you're correct those web sites of those companies that do have those types of sites, do have very large budgets... perhaps I will just let this go and take the easy way out (just having each vititor select their country, of course I would have default content for any country not listed specifically.:D :D ).

Thanks again for the reply!:) ;)

-devin

tech_support
10-08-2006, 02:34 AM
Try: http://www.phplocation.com/

But you will have to pay.


Edit: You can get the free version here: http://www.phplocation.com/ip-country/more_information.php?product_id=18,

djr33
10-08-2006, 02:50 AM
Also, if a user does select their location, you could use cookies/sessions or a database relating their IP to location so that you don't need to have them "sign in" each time....

However, the link that Tech just gave looks very promising. It might be a bit complex, but looks like it does exactly what you need, and it's from a company that has already dealt with the complexity of setting it up.... now you can just use it. Assuming the setup goes ok, I'd be very hopeful about that.
Good luck :)

devindevil
10-10-2006, 03:07 AM
Thanks Tech, I will definetely check that out.

By the way djr33, do you know if there is script for the cookies/sessions here @ dynamic drive? I am not to familiar with using cookies.

Thanks:p :D

-devin

tech_support
10-10-2006, 03:27 AM
http://www.devshed.com/c/a/JavaScript/Using-Cookies-With-JavaScript/

djr33
10-10-2006, 03:28 AM
I checked out the link and it was a big mess.
I had to literally buy the software, for zero euros (no credit card, though).
Then I had to unzip all that, look through the maze of files, to find that I need to "purchase" another free demo thing that allows me to decrypt the encrypted php scripts so I can view them.
I had other stuff to do, so I bailed at that point.

So... if you have the time to deal with that, looks quite promising... just annoying to get through all the commercial junk.

As for cookies/sessions, there isn't a script here (though there may be some javascripts dealing with cookies, but that's not relevant), mostly because DD is more javascript than php... almost entirely.
Sessions are really quite easy, and cookies aren't bad either (though they're not as good as sessions).

Sessions go something like this:
<?php
session_start(); //this continues/begins the session
//put session_start() at the top (or near the top, depending)
//on every page you want to session to exist on.

//now the $_SESSION[] array is available to you.

$_SESSION['IP_address'] = $_SERVER['REMOTE_ADDR']; //store user's IP
$_SESSION['name'] = $_POST['name']; //store 'name' from a form (method=post)
$_SESSION['currentpage'] = "index.php"; //store the page for the session
$_SESSION['var'] = $var; //etc.
//these are just examples... you can do anything you want.

?>

NEXT PAGE HERE:

<?php
//this is a new page...

session_start(); //remember to continue the session...

//with the session still going, we now have access to the old session vars.

echo $_SESSION['IP_address']; //user's IP, stored on the last page.
echo $_SESSION['var']; //should echo the value of $var from the last page.

session_destroy(); //for security, or whatever, time to get rid of the session.

?>

Notes:
Sessions aren't anything special. Really, they are just a great tool that PHP has to use different methods of storing data easily accessible. The use cookies for the most part, but using cookies alone causes problems if the browser isn't compatible with them or the user has them disabled. Therefore, the session will notice this and solve it by using GET (index.php?var=value&...) and/or POST data from forms, etc. This ensures that it should be compatible for everyone. More reliable than cookies.
One catch is that you must remember to continue (session_start()) the session each page, or there will be a hole in the chain and your data won't continue.

Sounds confusing, maybe, but it's really not so bad, and they're very useful.


Now, if you do get the above link/script to work, there's no need for this. If not, and you do have the user enter their location, then this would be helpful.

tech_support
10-10-2006, 03:32 AM
Sorry, that was a random Google search. Here's a code if you want JavaScript cookies (Highly Not Recommended):



/*
Script Name: Javascript Cookie Script
Author: Public Domain, with some modifications
Script Source URI: http://techpatterns.com/downloads/javascript_cookies.php
Version 1.0.0
Last Update: 30 May 2004

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/

// this function gets the cookie, if it exists
function Get_Cookie( name ) {

var start = document.cookie.indexOf( name + "=" );
var len = start + name.length + 1;
if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) )
{
return null;
}
if ( start == -1 ) return null;
var end = document.cookie.indexOf( ";", len );
if ( end == -1 ) end = document.cookie.length;
return unescape( document.cookie.substring( len, end ) );
}

/*
only the first 2 parameters are required, the cookie name, the cookie
value. Cookie time is in milliseconds, so the below expires will make the
number you pass in the Set_Cookie function call the number of days the cookie
lasts, if you want it to be hours or minutes, just get rid of 24 and 60.

Generally you don't need to worry about domain, path or secure for most applications
so unless you need that, leave those parameters blank in the function call.
*/
function Set_Cookie( name, value, expires, path, domain, secure ) {
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );
// if the expires variable is set, make the correct expires time, the
// current script below will set it for x number of days, to make it
// for hours, delete * 24, for minutes, delete * 60 * 24
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
//alert( 'today ' + today.toGMTString() );// this is for testing purpose only
var expires_date = new Date( today.getTime() + (expires) );
//alert('expires ' + expires_date.toGMTString());// this is for testing purposes only

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + //expires.toGMTString()
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}

// this deletes the cookie when called
function Delete_Cookie( name, path, domain ) {
if ( Get_Cookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

djr33
10-10-2006, 03:35 AM
No problem. Not complaining about you. The link looks very helpful, and probably is, once you can get around the mess of things to do with the setup. I hate now things are like that... so much work, for something. But it IS free, I suppose...

mwinter
10-10-2006, 11:07 AM
Try: http://www.phplocation.com/

The subject of IP-to-country resolution appeared fairly recently on Usenet (http://groups.google.co.uk/group/alt.html/browse_thread/thread/0b10914bae54733f/8ac6fd15f50daf5e#8ac6fd15f50daf5e) - it doesn't work reliably. There are plenty of ISPs that operate within multiple countries, and do not organise their IP addresses in such a way that allow one to distinguish one country from another. Alternatively, a user might have their traffic routed through a proxy that's in a different location than themselves, giving the appearance of being in that other location.

Consider, for instance, someone that's currently abroad when looking at your site (say, on a business trip). Would they really want information for their current location, or where they actually live?

If you really need to serve different content based on location, give the visitor the ability to choose. If the majority of your visitors will come from a certain region, you might want to default to that and let others select differently later.

Mike

djr33
10-10-2006, 11:42 AM
I think mike is right here.
Perhaps having the IP thing guess would be nice and give the viewers a feeling of home... not that, like so many sites, it just uses the default country.
You might also want to store that, so when they come back, it has that set. You could use a database and store by IP, fairly easily.