how to run javascript code for only US and UK visitors???
Moderator: General Moderators
how to run javascript code for only US and UK visitors???
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?
" 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?
- Josh1billion
- Forum Contributor
- Posts: 316
- Joined: Tue Sep 11, 2007 3:25 pm
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.
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.
- Josh1billion
- Forum Contributor
- Posts: 316
- Joined: Tue Sep 11, 2007 3:25 pm
- shiznatix
- DevNet Master
- Posts: 2745
- Joined: Tue Dec 28, 2004 5:57 pm
- Location: Tallinn, Estonia
- Contact:
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: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.
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;
}
?>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.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
-
d3ad1ysp0rk
- Forum Donator
- Posts: 1661
- Joined: Mon Oct 20, 2003 8:31 pm
- Location: Maine, USA
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.