PHP Code to Get IP address of Website Visitor
Knowledge about where the website is visited is important and how many visits are made by a same visitor. This can possible only by tracking IP address of the visitor, every internet user will have an unique IP address by this we can calculate from where our website is being visited.
Along with tracking of IP address, recording date and time also important. Because IP address for a user at day1 will be assigned to some other user at day2 or even in day1 itself.
Here is the PHP code to track the IP address and storing the IP address with its associated in datebase.
Tracking IP address can be achieved by $_SERVER['REMOTE_ADDR']
database table - tb
1 2 3 4 |
CREATE TABLE `mysql`.`tb` ( `date` VARCHAR( 15 ) NOT NULL , `ip` VARCHAR( 16 ) NOT NULL ) ENGINE = INNODB; |
db.php
1 2 3 4 |
<?php
$db=mysql_connect("localhost","root","");
mysql_select_db("mysql",$db);
?>
|
index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<html>
<body>
<?php
include('db.php');
$ip=$_SERVER['REMOTE_ADDR'];
$date=date('Y/m/d');
$query=mysql_query("insert into tb(date, ip) values('$date','$ip')");
echo "your IP address is".$ip;
if($query)
{
echo "Your visit is recorded in database";
}
else
{
echo "Error in recording of your entry";
}
?>
</body>
</html>
|
Output
Your IP address is 117206.100





Good spot Prasad! Actually I made one process for my blog site which also detects the IP addresses of the bots trying to access restricted folders I defined on my robots.txt.
I also used this coding but i did not get complete ip adress ….
Dr.Sharma, How have you been.
When i try this code…its showing like 127.0.0.1…..
not getting my IP…
You have tried from your PC, so that it shows this IP 127.0.0.1. Write the code to get IP address in a php file and upload it into server, then dynamic IP of visitors will be shown.
where ip address stores ….
in this tutorial, i have stored ip address in database
I am a junior php developer. But Prasad sir i have applied your code in my php script. When i apply in local machine then ip is shown 127.0.0.1…..But i upload all files in remote server. Is database also uploaded in server?? After that if any body visit my website then ip will be shown??
yes, you have to upload your database into server to store vistors ip address.