Archive

Posts Tagged ‘ip’

Set An IP Address From The Command Prompt In Windows

November 19th, 2008 No comments

Rather than use the old connection properties dialog in Windows you can open up a command prompt and use the netsh to set up all sorts of network specific settings. The most useful part of this is that you can create a bat file that will allow you to quickly change your local IP address very quickly.

To see a list of the network connections available you can use the following command.

netsh interface show interface

This runs the netsh program, in the interface context, and shows the interfaces available. There are lots of other contexts to chose from, just type netsh and then ? to see a list of commands. If you type in netsh and hit enter key you will see the prompt change to netsh meaning that you are in that program mode. Just type exit or bye to exit.

You can also ask for information about individual interfaces by using the name parameter.

netsh interface show interface name="Local Area Connection"

To inform a network interface that you want to change its IP address you need to call it by the network alias. Local Area Connection is usually the default, but if you have more than one network port (wired or wireless) then you should double check this.

To set the IP address of a network alias to run DHCP use the following.

netsh interface ip set address "Local Area Connection" dhcp

To set the IP address manually use the following.

netsh interface ip set address "Local Area Connection" static 192.168.0.10 255.255.255.0 192.168.0.1 1

The static parameter needs three items. The IP that the connection is to be set to, the network mask and the gateway.

In this way, if you travel a lot and want to turn on or off DHCP at the touch of a button then you can create a series of bat files that use these commands.

Categories: DOS Tags: , , , , , , , , , ,

Get The IP Address Of A Visitor Through PHP

November 12th, 2008 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 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: , , ,