My website presents a paid (not free) API. So I need to identify all incoming ajax calls and reject unknown requests. In other word, I only want to return a JSON result to:
- the request comes from my own website
- the request comes from a person who has paid the cost of API.
Here is my code:
$paid_ips = ['138.14.4.3', '32.16.6.1'];
$ip = $_SERVER['REMOTE_ADDR'];
if ( $ip == '::1' || in_array( $ip, $paid_ips) ) {
// allowed
} else {
// not allowed
}
As you know, $ip == '::1'
determines my website's requests. Now I want to know is what I'm doing secure? Or there is a better approach to handle that?