'Not getting correct IP address on the website

I am using https://ident.me/ for getting IP of anybody who visits my website. (https://notablee.co/get_ip)

But i am only getting the Shared Hosting IP (50.87.141.159) instead of the Visitors IP Address.

$ip = file_get_contents('https://ident.me/');


Solution 1:[1]

$ip=$_SERVER['REMOTE_ADDR'];

Fixed this. Thanks to 'Professor Abronsius' for a quick fix.

Solution 2:[2]

While your answer is not entirely wrong, you should consider that your code sits behind a loadbalancer or proxy. In this case, the $_SERVER array should contain the field X_HTTP_FORWARDED_FOR which contains a list of all previous IP addresses as a comma-separated string.

Also read this SO article How to use HTTP_X_FORWARDED_FOR properly?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Usman Malik
Solution 2 Honk der Hase