Get the Real-IP address of a visitor on your website

Advertisements

We are going to make use of three environment variables of PHP in order to code this simple PHP class that returns the real IP address of a visitor, even when connecting via a proxy. The three PHP environment variables we`ll use are

HTTP_CLIENT_IP:  gets the IP address of  Internet Service Provider.

HTTP_X_FORWARDED_FOR: gets the ip if the request is passed from a proxy. It will be empty if the pass is from a proxy.

REMOTE_ADDR: gets the IP address of  requesting client.

Now, with all the all the required variables explained let us code our PHP class.

function real_ip_address() {


	if (!empty($_SERVER['HTTP_CLIENT_IP'])){     //check if ip is from ISP
		$ip = $_SERVER['HTTP_CLIENT_IP'];
	}
	elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){     //check if ip is passed from a proxy
		$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
	}
	else{
		$ip = $_SERVER['REMOTE_ADDR'];
	}
	return $ip;
}

Now, the question is how to use this class? Copy the above code and paste it in a text file between the <?php – ?> tags. Save the file as realipaddress.php and place in your document root. Include this php file in your other code files by writing the statement

include(‘realipaddress.php’);

Now you can get the client`s IP address stored in a variable called $ip


Buzz This    



     Leave a Comment      Browse the Archives     


  • Attention webmaster! Post your advertisement to thousands of blogs (just like this one) for less than a penny per blog! We provide detailed reports of every blog posted on! To find out more, goto www.CommentNuke.info or email our sales rep at henryhavoc@gmail.com! Boost sales and rankings today!
  • @anonym, can you point us to a method in that case?
  • Hey! ben, just copy paste the above code and place at the top or bottom of your hidden page. The ip gets stored in $ip, you need to store the contents of thin a database or a file (txt)
  • anonym
    that cool;
    put there was not good if more pcs working on router :)

    thanks
  • Ben
    Debjit,

    I want to save the (real) ip's of visitors of an (hidden) webpage. How do I use the script above on my webpage? And where will the ip's be saved?

    I can make webpages but do not understand a lot of PHP.
blog comments powered by Disqus

Trackbacks