Tuesday, 10 December 2013

Server Variable Remote Address In Php

$_SERVER VARIABLE REMOTE
ADDRESS
Server Variable Remote address in php programming language. Remote address functionality of predefined $_Serverenvironmental information Now you properly want to know who website grab your ip addresses and its very simple you made think. Now in this example this will grab a ip addresses. But it will not be reliable. You should definitely not depend of using this method for grabbing users ip. So, its not a reliable way to grab a ip address. See my article better way to get Visitors ip addresses .
For example you may be having block list for the website of ip address you want to block from your website. In this post i will going you to show how to do this using this method but i will post better way to getting visitors ip address in php projects in correct way. So, get idea about this.
Code for $_SERVER variable Remote Address :-
Conf.inc.php (Screenshot 1)
$_Sever variable remote address
$_Sever variable remote address (Screenshot 1)
1
2
3
<?php
$ip_address = $_SERVER['REMOTE_ADDR'];
?>
index.php (Screenshot2)
$_Sever variable remote address
$_Sever variable remote address (Screenshot 2)
1
2
3
4
5
6
<?php
require 'conf.inc.php';
 
echo $ip_address;
 
?>
Output (Screenshot3):-
$_Sever variable remote address
$_Sever variable remote address (Screenshot 3)
In this example conf.inc.php contains a dynamic variable which will grab the users ip address and it will be updated based on user ip address. In this example we will get the users it and echo out on index.php and preview on our browser. In index.php file i had used require function. As you can see output is 127.0.0.1 is the localhostby default ip address. because i am running that localhost.
Code for $_SERVER Variable Remote Address
Conf.inc.php (Screenshot 4)
$_Sever variable remote address
$_Sever variable remote address (Screenshot 4)
1
2
3
4
<?php
$ip_address = $_SERVER['REMOTE_ADDR'];
$ip_blocked = array('127.0.0.1','100.100.100.100');
?>
index.php (Screenshot5)
$_Sever variable remote address
$_Sever variable remote address (Screenshot 5)
1
2
3
4
5
6
7
8
9
10
<?php
require 'conf.inc.php';
 
foreach($ip_blocked as $ip) {
 if($ip==$ip_address) {
 die('Your IP address,'.$ip_address.' had been blocked.');
 }
}
?>
<h1> Welcome!</h1>
Output (Screenshot 6) :-
$_Sever variable remote address
$_Sever variable remote address (Screenshot 6)
In this example i am blocking my self for example. getting my ip_address dynamically then putting static ip address which we want to block to visit our website for example i am putting localhostip address and random ip address like 100.100.100.100 into the conf.inc.php file. I had a Array for ip address which i want to block.
In index.php file i had foreach loop inside foreach construct we will check each ip address which we dynamically generated and the ip address static value which we stores in conf.inc.php $ip_blocked. will check from $ip_address if match found it will die the page . diefunction will kill the future page therefore its not showing h1 tags to us.

No comments:

Post a Comment