Jump to content

Redirect Based on OS, IP and Browser


dombrorj

Recommended Posts

Hi,

 

I'm trying to get a working php script that will redirect visitors based on a number of variables. I first want to check the country the visitor is from, and redirect based on the user's country ip address.

 

If visitor is not from the US, then go to google.com (for example)... but only after also checking some other variables. I want to also redirect any user (including US visitors) who are using a mobile device browser/cell phone, and redirect visitors coming from a Linux box.

 

I'm not sure how to add those as if/else statement. Any help would be appreciated. Here's what I have so far...

 

Geo ip Redirect:

-------------------

<?php

require_once("GeoIP.inc");

$gi = geoip_open("GeoIP.dat",GEOIP_STANDARD);

$country_code = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);

geoip_close($gi);

if($country_code == 'US')
{
        header('Location: http://www.google.com');
}
else { 
header("Location: http://www.redirect.com");
}
?>

 

Mobile Browser Redirect:

------------------------------

require_once('mobile_device_detect.php');
mobile_device_detect(true,false,true,true,true,true,true,'http://redirect.com',false);

 

 

So not sure how to detect and redirect Linux users, and then how to link these all together in one script so it checks all variables before sending user to destination URL.

 

Hope you can help. I really appreciate it!

Link to comment
https://forums.phpfreaks.com/topic/217180-redirect-based-on-os-ip-and-browser/
Share on other sites

Not sure on the specifics but with extensive if/else statements. Consider 'switch()' instead. Like so:

 

switch($country_code){

  case 'US':
    header() etc
    break;

  case 'UK':

    break;

}

 

Makes your code far more easier to manage.

 

You could put inside an object which would make it super easy to build and maintain. At very least, wrap everything in a function.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.