Well I have a simple code to send my US, Canada and UK traffic to one page and rest to another page. Could anyone post a simple php code to get this done ? Thanks
Oh, so how do you suggest to know where the users are coming from? The only way really is to use an IP database or a 3rd party service. Dan
I mean something like this but I'm not sure how to use IP address to find the country. $visitor = $_SERVER['REMOTE_ADDR']; if (preg_match("/111.111.111.1/",$visitor)) { header('Location: http://www.page01'); } else { header('Location: http://www.page02'); }; Code (markup):
You would need to know every ip address and what country it belonged to since there is no set system in IPv4 to define the country.
I don't Think i would need all since I'm filtering only few countries and rest will goto an other page as If( USA or CANADA ){ goto page01 } else{ goto page02 } What I don't know is how to use the IP Addresses for it. How to delect the country from an IP. .
<?php $country = geoip_country_code_by_name($_SERVER["REMOTE_ADDR"]); if (($country == "CN") or ($country == "IR") or ($country == "SA")) { header( 'Location: http://your-not-welcome-here.com' ) ; } ?> PHP: Of course change the country codes and the place you want them to go
Hmm I guess probably I explained it in a wrong way. What I want to know is how to use IPs for this code. I know the logic and how to use if problem is How do I indentify the country from a IP address. for example lets say a user comes from IP 173.32.13.95. and how i should know finds he is from CANADA ? I guess I have to check the first 3 digits. What I want to know is what is the best way to do it.
Maybe this will help then http://www.php.net/manual/en/reserved.variables.server.php http://www.php.net/reserved.variables All though using the code I gave will work. As page 1 would be the page you put the code in so if they belong there they stay if its a country you want to send to page 2 then add those countries to the code and it will send them there.
thank you guys actually i m also looking little for the same, i think i got enough resource from you....
I think that's exactly what he's looking for. This script below (Courtesy of Ip2Nation) should do the job perfectly. Just make sure you download the database and upload and import it your server. Then fill in the details below. <?php $server = ''; // MySQL hostname $username = ''; // MySQL username $password = ''; // MySQL password $dbname = ''; // MySQL db name $db = mysql_connect($server, $username, $password) or die(mysql_error()); mysql_select_db($dbname) or die(mysql_error()); $sql = 'SELECT country FROM ip2nation WHERE ip < INET_ATON("'.$_SERVER['REMOTE_ADDR'].'") ORDER BY ip DESC LIMIT 0,1'; list($country) = mysql_fetch_row(mysql_query($sql)); switch ($country) { case 'se': // Get the swedish to a swedish newssite header('Location: http://www.thelocal.se/'); exit; case 'us': // And let the folks from american go to CNN header('Location: http://www.cnn.com/'); exit; default: // The rest can go to BBC header('Location: http://www.bbc.co.uk/'); exit; } ?> PHP:
Yup this is what I was looking for. Thanks nabil_kadimi and qualityfirst ! Thanks everyone. Rep Added.
Actually, the best ip2country db is still from maxmind.. You can download the php functions to use it here: maxmind.com/app/php You'll need to download their db, just read the instructions. Then use the code by Mr. Bill. Also, make sure that you occassionally update your ip2country DB (even if you use ip2nation) as ips often times switch providers and could be assigned to other countries.