0

Hello can I anyone help me with this code to redirect user browsing me website from India to a different page.

var country = "India";/*Here I need to check the visitor country name please can anyone show me what to do*/
if (country == India){document.location='blocked-access.php'}
Siddharth Thevaril
  • 3,722
  • 3
  • 35
  • 71

4 Answers4

4

While this isn't an exact answer to the question, you should be doing the check in your PHP files instead. A JavaScript check that redirects to a "content blocked" page can be easily circumvented by the client. A serverside change cannot be tampered with to force displaying the blocked content (aside from a proxy/vpn).

jwkicklighter
  • 126
  • 1
  • 9
0

If client side check is okay (the redirection is not for security but for the user's convenience), you can use this.

 $.get("http://ipinfo.io", function (response) {
   console.log(JSON.stringify(response, null, 4)); //Here is user's country
}, "jsonp");
Andrea Dusza
  • 2,080
  • 3
  • 18
  • 28
0

Note: for better and for that you need to use server side scripting to find the location of the user and there is one extra thing that you need to do. for detail see below

First, you need to check the users ip address of the user visiting your site

if you are using C# then here how it's done

  string VisitorsIPAddr = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
    if (VisitorsIPAddr != null || VisitorsIPAddr != String.Empty)
    {
        VisitorsIPAddr = Request.ServerVariables["REMOTE_ADDR"];

    }
    var request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create("PathToTheWebResorceForExample_geobytes.com");

Second thing that you need to check the location against user ip address.

Your request will be something like this

http://getcitydetails.geobytes.com/GetCityDetails?fqcn=Visitors_IP_ADDRESS

You can do this from site like this http://www.geobytes.com/iplocator/

The request will return json that will include city, country and related information.

Then if you are using C#, the you need to use JsonConvert class to parse and get the location.

Then if user from India then redirect him to whichever page you like.

user786
  • 3,902
  • 4
  • 40
  • 72
0

Here is solution with pure javascript (using JSONP):

1.) Declare this function in script where country name is needed ->

   function jsonpCallback(data){ 

    alert(data.address.country); 
   }

2.) Call this script in your HTML file ->

<script src="http://api.wipmania.com/jsonp?callback=jsonpCallback" type="text/javascript"></script>

The script above fetches data from server and moves them into your callback function as data object which contains informations about user location.

Jay Sky
  • 133
  • 1
  • 1
  • 8