I want to prevent people from certain countries from visiting my website - but I want to allow some defined IP addresses from those countries to be able to access it. Is it possible? If yes, how?
You could deny access to a range of IP addresses known to originate from that country, while using a white list to enable access to those specific IP addresses which you want to allow. However, this could easily be subverted by, for example, using a proxy from a different country, or simply by going to a different country and then viewing the website from there. This is probably more trouble than it is worth.
Also, you might want to verify that you are legally permitted to deny access on a country-wide basis. Not to mention, of course, the ethical implications of discriminating against users on the basis of nationality.
You could use some data like this: http://www.ipinfodb.com/
When you have that in place, you check to see if your guest matches a banned location, and if they do, only allow them if they are on a white list of ips.
Of course, nothing you do will stop people from using a proxy if they really want to.
The easiest way to solve this problem, using data like this(Click download on right-bottom of webpage) for converting user ip to country.