Scripts / Get the client IP address in PHP php
Description

It's often useful to record the API client's IP address. PHP has long supported an easy way to retrieve the IP address using the $_SERVER['REMOTE_ADDR'] variable. Keep in mind however that the client is sometimes operating behind a proxy, and that proxy might therefore assign the IP address to the HTTP_X_FORWARDED_FOR header. PHP will additionally make this value available using the $_SERVER['HTTP_X_FORWARDED_FOR'] variable.

There is no additional configuration required to access PHP's global $_SERVER variable array.

Code
$ip = $_SERVER['REMOTE_ADDR'];
$proxiedIP = $_SERVER['HTTP_X_FORWARDED_FOR'];

Need API Advice?

Our team has advised thousands of companies around the world on API projects. Go to market faster by talking to the API experts.

jeanie

Ready to get started?