1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

A code for redirecting traffic from different countires

Discussion in 'PHP' started by bluearrow, Dec 30, 2008.

  1. #1
    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 ? :)
    SEMrush

    Thanks
     
    bluearrow, Dec 30, 2008 IP
    SEMrush
  2. proxywhereabouts

    proxywhereabouts Notable Member

    Messages:
    4,027
    Likes Received:
    110
    Best Answers:
    0
    Trophy Points:
    200
    #2
    proxywhereabouts, Dec 30, 2008 IP
    bluearrow likes this.
  3. bluearrow

    bluearrow Well-Known Member

    Messages:
    1,339
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    130
    #3
    way too complicated. i think there are lot more simple way to get this done.


    .
     
    bluearrow, Dec 30, 2008 IP
  4. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #4
    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
     
    Danltn, Dec 30, 2008 IP
    bluearrow likes this.
  5. bluearrow

    bluearrow Well-Known Member

    Messages:
    1,339
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    130
    #5
    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):
     
    bluearrow, Dec 30, 2008 IP
  6. qualityfirst

    qualityfirst Peon

    Messages:
    147
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    0
    #6
    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.
     
    qualityfirst, Dec 30, 2008 IP
  7. bluearrow

    bluearrow Well-Known Member

    Messages:
    1,339
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    130
    #7
    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.

    .
     
    bluearrow, Dec 30, 2008 IP
  8. Mr.Bill

    Mr.Bill Well-Known Member

    Messages:
    2,818
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    160
    #8
    <?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
     
    Mr.Bill, Dec 30, 2008 IP
  9. bluearrow

    bluearrow Well-Known Member

    Messages:
    1,339
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    130
    #9
    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.
     
    bluearrow, Dec 30, 2008 IP
  10. Mr.Bill

    Mr.Bill Well-Known Member

    Messages:
    2,818
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    160
    #10
    Mr.Bill, Dec 30, 2008 IP
    bluearrow likes this.
  11. nabil_kadimi

    nabil_kadimi Well-Known Member

    Messages:
    1,065
    Likes Received:
    69
    Best Answers:
    0
    Trophy Points:
    195
    #11
    nabil_kadimi, Dec 31, 2008 IP
    bluearrow likes this.
  12. seo_specialist

    seo_specialist Guest

    Messages:
    106
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #12
    thank you guys actually i m also looking little for the same, i think i got enough resource from you....
     
    seo_specialist, Dec 31, 2008 IP
  13. qualityfirst

    qualityfirst Peon

    Messages:
    147
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    0
    #13
    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:
     
    qualityfirst, Dec 31, 2008 IP
    bluearrow likes this.
  14. bluearrow

    bluearrow Well-Known Member

    Messages:
    1,339
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    130
    #14
    Yup this is what I was looking for. Thanks nabil_kadimi and qualityfirst !

    Thanks everyone. Rep Added.
     
    bluearrow, Jan 1, 2009 IP
    nabil_kadimi likes this.
  15. improvingtheweb

    improvingtheweb Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    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.
     
    improvingtheweb, Jan 1, 2009 IP