$ip = $_SERVER['REMOTE_ADDR'] ? : ($_SERVER['HTTP_X_FORWARDED_FOR'] ? : $_SERVER['HTTP_CLIENT_IP']);


References

  1. http://itman.in/en/how-to-get-client-ip-address-in-php/
  2. https://stackoverflow.com/questions/3003145/how-to-get-the-client-ip-address-in-php
  3. https://www.virendrachandak.com/techtalk/getting-real-client-ip-address-in-php-2/