how to run javascript code for only US and UK visitors???

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
softsoft
Forum Newbie
Posts: 2
Joined: Tue Oct 16, 2007 3:57 am

how to run javascript code for only US and UK visitors???

Post by softsoft »

i have a PHP site and i would like to run a javascript code for only US and UK visitors.

" if visitor from US or UK the javascript code working and if the visitor from any other country the code didn't work "

how can i do that?
User avatar
Josh1billion
Forum Contributor
Posts: 316
Joined: Tue Sep 11, 2007 3:25 pm

Post by Josh1billion »

You want to print the script for US and UK visitors only, and print nothing/something else for other visitors?

Here's something which can determine the country of the visitor: http://www.phptutorial.info/iptocountry/the_script.html

Then just print out the script if the country is US or UK.
softsoft
Forum Newbie
Posts: 2
Joined: Tue Oct 16, 2007 3:57 am

Post by softsoft »

can i get complete modified code please, because my PHP knowledge is too low and i gust start learning PHP

Thanks for your help.
User avatar
Josh1billion
Forum Contributor
Posts: 316
Joined: Tue Sep 11, 2007 3:25 pm

Post by Josh1billion »

I haven't completely tested it, but I think all you need to do is insert the country codes into the database (with that attachment) and then run that PHP script.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

softsoft wrote:can i get complete modified code please, because my PHP knowledge is too low and i gust start learning PHP

Thanks for your help.
Its super easy. Download this zip (from their site) http://www.phptutorial.info/iptocountry/ip_files.zip then unzip it and you will have a folder called ip_files. Take that whole folder and put it on your webserver (probably in your webroot). then take this code:

Code: Select all

<?
$IPaddress=$_SERVER['REMOTE_ADDR'];
$two_letter_country_code=iptocountry($IPaddress);

if ($two_letter_country_code=="US" || $two_letter_country_code=="UK"){
    echo '<script...>'; //<-- THIS IS THE SCRIPT FOR US AND UK USERS!!!
}

function iptocountry($ip) {   
    $numbers = preg_split( "/\./", $ip);   
    include("ip_files/".$numbers[0].".php");
    $code=($numbers[0] * 16777216) + ($numbers[1] * 65536) + ($numbers[2] * 256) + ($numbers[3]);   
    foreach($ranges as $key => $value){
        if($key<=$code){
            if($ranges[$key][0]>=$code){$two_letter_country_code=$ranges[$key][1];break;}
            }
    }
    if ($two_letter_country_code==""){$two_letter_country_code="unkown";}
    return $two_letter_country_code;
}
?>
and put that into where you want the script to be. Find the comment in there that says "THIS IS THE SCRIPT FOR US AND UK USERS!!!" and edit that echo accordingly.

I just checked this and it did prove correct so... yay! Just remember that this is not going to always work. There is a lot of reasons for this to give a false positive or a false negative. Just don't rely on this as being correct always.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Understand that IPs are very unreliable identifiers, do this may do exactly what you want all the time.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

Everah wrote:Understand that IPs are very unreliable identifiers, do this may do exactly what you want all the time.
deep breath, try again :lol:
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I think what I was supposed to say was

Doing this may not do what you expect it to do all the time.

I really need to read my posts better.
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

If you want to be sure (in the sense that no one in the wrong country accidentily), a better solution would simply be to ask them where they're from. Note how most international sites provide an interface for choosing your country? It's mainly for Everah's reasoning, that IPs aren't always a great way to determine someone's location.
Post Reply