Page 1 of 1

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

Posted: Tue Oct 16, 2007 4:04 am
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?

Posted: Tue Oct 16, 2007 4:41 am
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.

Posted: Tue Oct 16, 2007 5:40 am
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.

Posted: Tue Oct 16, 2007 6:01 am
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.

Posted: Tue Oct 16, 2007 7:29 am
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.

Posted: Tue Oct 16, 2007 11:31 am
by RobertGonzalez
Understand that IPs are very unreliable identifiers, do this may do exactly what you want all the time.

Posted: Tue Oct 16, 2007 12:09 pm
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:

Posted: Tue Oct 16, 2007 2:03 pm
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.

Posted: Tue Oct 16, 2007 3:07 pm
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.