I have no idea, how to make a post visible only to one or 2 countries. How will the code gets to know, from which country the user is? Give me some hints and tips. It should be without making any account.
Asked
Active
Viewed 209 times
0
-
1I have seen this somewhere before, is this the current homework project – RiggsFolly Jul 14 '16 at 16:46
-
here is a scenario user enters the website we check with his ip address if he from x country you allow him to view the post if he from y country you don't allow him – Ahmad ghoneim Jul 14 '16 at 16:48
-
Possible duplicate of [Get Country of IP Address with PHP](http://stackoverflow.com/questions/3650006/get-country-of-ip-address-with-php) – Jul 14 '16 at 16:49
4 Answers
1
There are a lot of server variables like $_SERVER["REMOTE_ADDR"], $_SERVER['HTTP_CLIENT_IP'] etc by which you can find the logged in user's IP address.
Check out: PHP Server variables
Once you retrieve those, there are quite a few API's which could be used to determine the user's country.
You can use these as references:

Community
- 1
- 1

Indrasis Datta
- 8,692
- 2
- 14
- 32
0
You can determine their country from their ip. Check this site out: http://ipinfo.io/.
$.get("http://ipinfo.io", function(response) {
if (response.country == 'whatever country') {
// show whatever content
}
}, "jsonp");

bill
- 1,646
- 1
- 18
- 27
-
Not sure if writing client side code for such things are a good idea. It's better to keep your business logic on the server side. – Indrasis Datta Jul 14 '16 at 16:54
-
In this case it's probably fine, if somebody's going to be doing malicious things to hide their IP address server-side validation doesn't beat a proxy – Caius Jul 14 '16 at 16:59
0
Apply the IP out of Server into an API. The manual describes one such set of functions. http://php.net/manual/en/book.geoip.php

gladiola
- 133
- 5
0
there are several options
- build your own IP to country database and application to detect country
- use API service like http://ipinfo.io, http://ip-api.com/docs/ etc...
- host with cloudflare and use
$country_code = $_SERVER["HTTP_CF_IPCOUNTRY"];
https://support.cloudflare.com/hc/en-us/articles/200168236-What-does-CloudFlare-IP-Geolocation-do-

uingtea
- 6,002
- 2
- 26
- 40