Thursday, 20 March 2014

Server Variable Remote Address

php ip addresses
Server Variable Remote address in php programming language. Remote address functionality of predefined $_Server environmental 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 grape a ip addresses. But it will not be reliable. So, its not a reliable way to grape a ip address. So, You should definitely not depend of using this method for graphing users ip.

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)

Server Variable Remote Address
conf.inc.php Screenshot1


<?php

$ip_address = $_SERVER['REMOTE_ADDR'];

?>

index.php (Screenshot2)

index.php Screenshot2


<?php

require 'conf.inc.php';



echo $ip_address;



?>

Output (Screenshot3):-

server variable remote address
Screenshot3
In this example conf.inc.php contains a dynamic variable which will grape 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 localhost by default ip address. because i am running that localhost.

Code for $_SERVER Varibale Remote Address 

Global Variable in php
Code for $_SERVER Variable Remote Address Screenshot4


<?php

$ip_address = $_SERVER['REMOTE_ADDR'];

$ip_blocked = array('127.0.0.1','100.100.100.100');

?>

index.php (Screenshot5)

ip addresses Blocking in php
index.php Screenshot5



<?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) :-

$_SERVER Ip addresses
Screenshot6
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 localhost ip 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 . die function will kill the future page therefore its not showing h1 tags to us. 


No comments:

Post a Comment

Thanks For Comment Will get you Soon..

About Me

Welcome to Extra Tutorials! My name is Mohammed and I am the 22 year writer, website developer, and photographer behind the blog. Thanks for visiting! Tutorials Jackpot… In addition to Developer, I love to develop websites and I love to write. Starting a php Blog was inevitable for me. What began as a simple way to share all of my Tutorials with friends and family has developed into my Part time job.

Mohammed Padela

WHAT IS PHP PROGRAMMING

WHAT IS PHP PROGRAMMING
WHAT IS PHP PROGRAMMING

Follow Us

Popular Posts

Designed ByBlogger Templates