I have been using the following PHP code to record IPs:
getenv('HTTP_CLIENT_IP') ?: getenv('HTTP_X_FORWARDED_FOR') ?: getenv('REMOTE_ADDR')
It had been working fine. But starting this May, I noticed that all recorded IPs are 74.220.219.56. I also tested it by using VPN and changing country, and the problem persists. Now I have to use only getenv('REMOTE_ADDR')
in order to get the correct IP.
Could that be caused by my hosting company's settings?
HTTP_CLIENT_IP
and HTTP_X_FORWARDED_FOR
are both HTTP headers that are non-standard and have to be set by a proxy server. If your hosting company has a proxy sitting in front of your PHP application, they need to enable these headers for this to work.
Furthermore, you should only ever read either of these headers for the IP if you are absolutely sure that your hosting provider uses them. If you don't know sure, this could be a security issue.
So in short, whether either of those headers work is entirely dependent on your hoster, and you should use neither of these headers until you find out if your hoster supports them.
If you are working PHP webs application then you will be able to check user IP by super global server variable.
$ip = $_SERVER["REMOTE_ADDR"];
If you call print_r function on $_SERVER variable, you will see related information about server. Here is out for Local server (wamp)
Array
(
[HTTP_HOST] => localhost:8080
[HTTP_CONNECTION] => keep-alive
[HTTP_UPGRADE_INSECURE_REQUESTS] => 1
[HTTP_USER_AGENT] => Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36
[HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
[HTTP_ACCEPT_ENCODING] => gzip, deflate, br
[HTTP_ACCEPT_LANGUAGE] => en-US,en;q=0.9
[HTTP_COOKIE] => PHPSESSID=f1m56d9il08q2no5trdvdb4us1; _ga=GA1.1.1485216858.1538653842; user_ip=%3A%3A1; th_active_theme=USD; wordpress_test_cookie=WP+Cookie+check
[PATH] => C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Microsoft SQL Server\120\DTS\Binn\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\120\Tools\Binn\ManagementStudio\;C:\Program Files (x86)\Microsoft SQL Server\120\DTS\Binn\;C:\wamp64\bin\php\php5.6.25;C:\Program Files\Git\cmd;C:\WINDOWS\System32\OpenSSH\;C:\composer;C:\Program Files\PuTTY\;C:\Program Files
odejs\;C:\WINDOWS\system32\config\systemprofile\AppData\Local\Microsoft\WindowsApps
[SystemRoot] => C:\WINDOWS
[COMSPEC] => C:\WINDOWS\system32\cmd.exe
[PATHEXT] => .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
[WINDIR] => C:\WINDOWS
[SERVER_SIGNATURE] =>
Apache/2.4.23 (Win64) PHP/7.0.10 Server at localhost Port 8080
[SERVER_SOFTWARE] => Apache/2.4.23 (Win64) PHP/7.0.10
[SERVER_NAME] => localhost
[SERVER_ADDR] => ::1
[SERVER_PORT] => 8080
[REMOTE_ADDR] => ::1
[DOCUMENT_ROOT] => C:/wamp64/www
[REQUEST_SCHEME] => http
[CONTEXT_PREFIX] =>
[CONTEXT_DOCUMENT_ROOT] => C:/wamp64/www
[SERVER_ADMIN] => wampserver@wampserver.invalid
[SCRIPT_FILENAME] => C:/wamp64/www/test/index.php
[REMOTE_PORT] => 60364
[GATEWAY_INTERFACE] => CGI/1.1
[SERVER_PROTOCOL] => HTTP/1.1
[REQUEST_METHOD] => GET
[QUERY_STRING] =>
[REQUEST_URI] => /test/
[SCRIPT_NAME] => /test/index.php
[PHP_SELF] => /test/index.php
[REQUEST_TIME_FLOAT] => 1558071651.052
[REQUEST_TIME] => 1558071651
)