@jordan-thirkleJan 25.2023 — #The most reliable way to get a user's IP address in PHP is by using the:
$_SERVER['HTTP_CLIENT_IP'] variable,
followed by: $_SERVER['HTTP_X_FORWARDED_FOR'] and then $_SERVER['REMOTE_ADDR']. This is because some users may be behind a proxy, and these variables account for that by providing the IP address of the client, the IP address of the proxy, and the IP address of the user respectively.
It would be best practice to check for the existence of each variable before using it, and then use the first non-empty value.
Please note: that the above method may not work if the user is behind a VPN or a corporate network.
You may consider using an external service or library, if more robust IP detection is needed.
Hope this helps!
@jordan-thirkleThe method I described above, which involves checking for the existence of the $_SERVER['HTTP_CLIENT_IP'], $_SERVER['HTTP_X_FORWARDED_FOR'], and $_SERVER['REMOTE_ADDR'] variables in that order, is considered to be one of the most reliable methods for getting a user's IP address in PHP. This method accounts for the possibility that the user may be behind a proxy, and it returns the IP address of the client, the IP address of the proxy, and the IP address of the user respectively.Jan 25.2023
@ElBiastoJan 18.2023 — #The most reliable way to get a user's IP address in PHP is to use the $_SERVER['REMOTE_ADDR'] variable, which contains the IP address of the user that is making the request to the server. However, it is important to note that this may not always be accurate, as some users may be behind a proxy or VPN. In those cases, you can use the $_SERVER['HTTP_X_FORWARDED_FOR'] variable to get the user's IP address, but it is important to validate the IP address to ensure that it is not being spoofed.