Web Development and Design | Tutorial for Java, PHP, HTML, Javascript: IP

Web Development and Design | Tutorial for Java, PHP, HTML, Javascript: IP
Showing posts with label IP. Show all posts
Showing posts with label IP. Show all posts

PHP To Get Current User (Client) IP Address


PHP To Get Current User (Client) IP Address

php get user ip


Some Times We Need To Get Our Current User Ip Address Here is a PHP function We can Get the user ip easily without doing anything just call the function.

Code : -
<?php
function ip_address()
{
foreach (array('HTTP_CLIENT_IP',
'HTTP_X_FORWARDED_FOR',
'HTTP_X_FORWARDED',
'HTTP_X_CLUSTER_CLIENT_IP',
'HTTP_FORWARDED_FOR',
'HTTP_FORWARDED',
'REMOTE_ADDR') as $key){
if (array_key_exists($key, $_SERVER) === true)
{
foreach (explode(',', $_SERVER[$key]) as $ipaddress)
{
$ipaddress = trim($ipaddress); // Just to be safe

if (filter_var($ipaddress,
FILTER_VALIDATE_IP,
FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)
!== false) {

return $ipaddress;
}
}
}
}
}
echo ip_address();

Thanks.
2