Archive

Posts Tagged ‘address’

Get The IP Address Of A Visitor Through PHP

November 12th, 2008 Tech 1 comment

I have talked previously about getting an IP address of a visitor with PHP. The failing in using the value of $_SERVER['REMOTE_ADDR'] is that if the visitor is using a proxy then you will get the proxy IP address and not the visitors real IP address.

This function works by going through any variables in the $_SERVER array that might exist that would contain information to do with IP addresses. If they are all empty then the function finally looks at $_SERVER['REMOTE_ADDR'] value and returns this as a default.

function getRealIpAddr(){
 if ( !empty($_SERVER['HTTP_CLIENT_IP']) ) {
  //check ip from share internet
  $ip = $_SERVER['HTTP_CLIENT_IP'];
 } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']) ) {
  //to check ip is pass from proxy
  $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
 } else {
  $ip = $_SERVER['REMOTE_ADDR'];
 }
 return $ip;
}

To run this function just call it.

echo getRealIpAddr();

This function was originally found here.

Categories: PHP Tags: , , , , ,

Blocking Multiple IP Addresses With PHP

May 20th, 2008 Tech No comments

It is sometimes necessary to block people from using your site, dependent on their IP address. A users IP address can be detected by PHP using the $_SERVER superglobal and the parameter REMOTE_ADDR.

The code includes two ways to load the list of IP addresses. The first is by hard coding it into an array, and the second is by the use of a plain text file called "blocked_ips.txt". The format of this file is simply a list of IP addresses, with one address on each line. Through the use of the file() function this file is loaded as an array into of addresses.

if(!file_exists('blocked_ips.txt')){
 $deny_ips = array(
  '127.0.0.1',
  '192.168.1.1',
  '83.76.27.9',
  '192.168.1.163'
 );
}else{
 $deny_ips = file('blocked_ips.txt');
}
// read user ip adress:
$ip = isset($_SERVER['REMOTE_ADDR']) ? trim($_SERVER['REMOTE_ADDR']) : '';
 
// search current IP in $deny_ips array
if((array_search($ip, $deny_ips))!== FALSE){
 // address is blocked:
 echo 'Your IP adress ('.$ip.') was blocked!';
 exit;
}

There are two things you should be aware of when using this method.
The first is that a users IP address can change due to many factors. They could either move to a different building or their ISP could assign a different IP address for them.
The second thing to be aware of is that you should be careful when entering IP addresses so that you don’t block search engine spiders from looking at your site. Instead of spidering your content they will just index the phrase "Your IP address (0.0.0.0) was blocked!"

Categories: PHP Tags: , , ,

Mask Email With ASCII Character Codes In PHP

April 28th, 2008 Tech No comments

Hiding your email address in an image is the best way of encrypting your email, but if your server doesn’t support the GD2 library, or if you don’t want to use it, then you might want to look at a different way of doing this.

The easiest way to encrypt your email address is to turn every character into the ASCII code equivalent and use this to display the text in HTML by putting a &# in front of each character. Here is a function that takes a string and turns it into HTML encoded text.

function maskEmail($email){
 $hiddenEmail = '';
 $length = strlen($email);
 
 for($i = 0;$i<$length;$i++){
  $hiddenEmail .= "&#".ord($email[$i]).";";
 }
 
 return $hiddenEmail;
}

To use this just feed an email address into it.

$hiddenEmail = maskEmail("example@example.com");
echo $hiddenEmail;

This produces the following output in the source code (the browser will display this string as the email address you put in.

&#101;&#120;&#97;&#109;&#112;&#108;&#101;&#64;&#101;&#120;&#97;&#109;&#112;&#108;&#101;&#46;&#99;&#111;&#109;

This can be easily added to a anchor tag to make an email link.

echo '<a href="mailto:'.$hiddenEmail.'" title="Email me">'.$hiddenEmail.'</a>';

Categories: PHP Strings Tags: , , , ,