| Better Way To Get Visitors Ip Address |
Get visitors ip address in php programming language. In thearticles of $_server variables host name , $_server variables script name , $_server variables Remote address . We seen $_SERVER['REMOTE_ADDR'] but its not the better way to grab the visitors ip address because they may be using proxy address. So, In this php project we will check there arguments we will check three elements the first element the actual ip address of visitors then if visitors on proxy ip address will check such type of conditions in this script of php programming languages.
Code for get visitors ip address (Screenshot1):-
| Code for get visitors ip address (Screenshot1) |
<?php$http_client_ip = $_SERVER['HTTP_CLIENT_IP'];$http_x_forwarded_for = $_SERVER['HTTP_X_FORWARDED_FOR'];$remote_addr = $_SERVER['REMOTE_ADDR'];if(!empty($http_client_ip)){ $ip_address = $http_client_ip;}else if(!empty(http_x_forwarded_for)){ $ip_address = $http_x_forwarded_for;}else{ $ip_address = $remote_addr;}echo $ip_address;?>
Output (Screenshot2):-
|
| Output (Screenshot2) |
In this we had checking visitors ip address.
$http_client_ip = $_SERVER['HTTP_CLIENT_IP'];
This line of code is will check actual internet ip address of the visitors rather then individual computer.
$http_x_forwarded_for = $_SERVER['HTTP_X_FORWARDED_FOR'];
This line of code is will check whether visitors is using proxy ipaddress or not.
$remote_addr = $_SERVER['REMOTE_ADDR'];
This line of code stands for Server Variable Remote Address.if(!empty($http_client_ip)){ $ip_address = $http_client_ip;}else if(!empty($http_x_forwarded_for)){ $ip_address = $http_x_forwarded_for;}else{ $ip_address = $remote_addr;}echo $ip_address;
?>
This line of code in if else if condition we are checking whether three conditions are not empty and storing in variable $ip_address. Here output in screenshot2 it will be localhost ip address because here i am running on localhost.
You can also test this script by uploading on webservers connected to internet test where were you want. This is lot better way to get visitors ip address just by checking three different possibilities.
Read More Extra Tutorials
No comments:
Post a Comment