How to get the user ip address in php

In many situations developers want's the user ip address.

Those are
In user registration time store his/her ip address.
In login time also store his/her ip address .

In these two factors developer compares the registered ip address and logged ip address. If both are equal no issue when these are not equal then intimate the user "You are logined the unknown ip address".

In other situation when developers want's to block the some of the users ip.
Because of user voilating the site rules webmaster can restrict the access to the perticular ip address.

Some situations completely block the site access to the particular ip address.

This is a simple script to get the users ip address using the php.
 
<?php  
  $client = @$_SERVER['HTTP_CLIENT_IP'];  
  $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];  
  $remote = @$_SERVER['REMOTE_ADDR'];  
  if(filter_var($client, FILTER_VALIDATE_IP)){  
  $ip = $client;  
  }elseif(filter_var($forward, FILTER_VALIDATE_IP)){  
  $ip = $forward;  
  }else{  
  $ip = $remote;  
  }  
 echo $ip;  
?>

* If you like this post please don’t forget to subscribe Techies Badi - programming blog for more useful stuff


EmoticonEmoticon