get ip address in php - Web Development and Web Design Codes

Latest

Tuesday, April 3, 2018

get ip address in php

get ip address in php

get ip address in php
get ip address in php
Hi Friends in this lesson we are going to learn how to get visitor ip address and website host name using php...
in many times we need to collect user ip address to track user activity and other security reason..you can get user ip address easily using php..
 $_SERVER['REMOTE_ADDR'] are use to Detect ip address of a user..
you can print current user ip by the below method
<?php
echo $_SERVER['REMOTE_ADDR'];
?>

but sometimes $_SERVER['REMOTE_ADDR'] method can't detect the current user ip address ..because they are using vpn or proxy..
You can use below codes to detect user ip address..
ip.php
<?php
function getUserIP(){
    if(!empty($_SERVER['HTTP_CLIENT_IP'])){
        //Check if ip from share INTERNET
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    }elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
        //check ip ip pass from proxy
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    }else{
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}
//Get Host Name
$hostname = getenv('HTTP_HOST');
echo 'You IP:--- '.getUserIP();
echo '<br/>Your Host Name:-- '.$hostname;
?>

That's it Friends how to get ip address in php..i hope you enjoy this article  ..Please share with your friends ..Thank You...

No comments:

Post a Comment

Thank You for Your Comment

Note: Only a member of this blog may post a comment.