My problem is: I need to restrict access to people from another country such as Japan . As I do so that no one in Japan can go to my website ?
-
You should implement a event listener listens to Controller event and do all your logic Refer http://stackoverflow.com/questions/12553160/getting-visitors-country-from-their-ip and http://symfony.com/doc/current/cookbook/event_dispatcher/before_after_filters.html – Tuan nguyen Jul 03 '15 at 04:20
2 Answers
You will have to detect IP of the user and redirect the user to an error page if the IP is from Japan.
A nice bundle to detect user IP is available here:
https://github.com/aferrandini/Maxmind-GeoIp
To block all you pages at once you could do the IP check inside a Listner that define the method onKernelRequest. An example visible here : http://symfony.com/doc/2.3/cookbook/service_container/event_listener.html#request-events-checking-types
Don't forget to register the listener in the services.yml file available in each bundle.
An alternative way to detect japanese users could be to parse the user agent received from the browser and parse it, it will probably contains "ja" or "JP" as a language marker.

- 107
- 6
Use http://ipinfo.io/ API. The code looks like: (PHP)
$allowed_countries = array("IN", "US", ...);
$country = file_get_contents("http://ipinfo.io/{$_SERVER['REMOTE_ADDR']}/country");
if(!in_array($country, $allowed_countries))
{
echo 'Restricted'; //or any error message.
}

- 2,400
- 4
- 18
- 32
-
1Caching IP addresses (or storing them in a DB) and checking your cache (or DB) before making the call each time could speed up page calls. – qooplmao Jul 03 '15 at 05:37