如何使帖子只显示1个或2个国家[关闭]

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.

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:

  1. Getting visitors country from their IP

  2. Get Country of IP Address with PHP

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");

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

there are several options

  1. build your own IP to country database and application to detect country
  2. use API service like http://ipinfo.io, http://ip-api.com/docs/ etc...
  3. 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-