How to get ip address using PHP

Select your language

How to copy a MySQL row within the same table using PHP

Problem: You are writing a web application or need to note your visitor IP address into database, how you will get his/her IP?

Solution: Getting IP address using PHP is not difficult, but this single function will help you to get IP address of visitor in proper and safe way. Please note it.

Function to get IP address
PHP code here:
<?php

// Function to get the client IP address public function get_client_ip() { $ipaddress = ''; if (getenv('HTTP_CLIENT_IP')) $ipaddress = getenv('HTTP_CLIENT_IP'); else if(getenv('HTTP_X_FORWARDED_FOR')) $ipaddress = getenv('HTTP_X_FORWARDED_FOR'); else if(getenv('HTTP_X_FORWARDED')) $ipaddress = getenv('HTTP_X_FORWARDED'); else if(getenv('HTTP_FORWARDED_FOR')) $ipaddress = getenv('HTTP_FORWARDED_FOR'); else if(getenv('HTTP_FORWARDED')) $ipaddress = getenv('HTTP_FORWARDED'); else if(getenv('REMOTE_ADDR')) $ipaddress = getenv('REMOTE_ADDR'); else $ipaddress = 'UNKNOWN'; return $ipaddress; }
?>

 That's all ! If you not familiar with object oriented, no problem, contact me today and I will help your solve your problem