Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

GeoIP redirect conditional

Hello guys!, my first post, I hope you could help me. I'm a beginner with php. I want to try to make a conditional for a redirection depending of your country to your respective language, but I just want to do it once. I don't know how the code should be write to make a conditional with the cookies and just get redirect once. Did I explain myself? :S

1 Answer

Hi Raul

The first thing I would probably recommend is to check out some of the many php plug-in scripts out there on the internet which might help you with this.

If I understand your question right, you want to:

  1. Have your site detect which country a visitor is from
  2. Redirect them to a version of your site in their native tongue
  3. Save a cookie so that the next time they revisit your site they don't have to go through this again?

The issue with this is that there isn't really a true sure-fire way to obtain the "real I.P address" of a visitor.

The reason for this is that you can only get the real I.P from a proxy if that proxy is not fully anonymous otherwise all you will get is the proxy's I.P address not the address of the actual visitor.

That being said, if you were to get "a" I.P address that you believe is the visitors, there are many third party APIs that offer solutions to lookup and match the I.P address in question to the country of origin for that I.P

At it's very basic level, to get what you can assume is a visitor's I.P address (bearing in mind what I have already said about proxies typically not giving up the true I.P address) you could go....

<?php

$visitor_ip_cookie =$_SERVER['REMOTE_ADDR'];
echo "IP address= $visitor_ip_cookie"; // Just as an example this prints out the I.P address of a visitor

?>

If you wanted to set the cookie of that visitor for example and then check if that cookie exists when they revisit the page, you could do something similar to this...

<?php

// First we have previously set a cookie for the visitor's supposed I.P address like so....
$visitor_ip_cookie = $_COOKIE["visitor_ip_cookie"];

// Then we check to see if the cookie is not already set, if they are a first time visitor, this cookie is then set
if(!isset($_COOKIE['lvisitor_ip_cookie'])) {
   setcookie("visitor_ip_cookie", $visitor_ip_cookie);
}
else {
  // ... some code here to redirect them depending on which I.P address they have
}

?>

Not the most perfect example but you get the idea

Happy coding!