New to our community ?

Discover a world of possibilities! Join us and explore a vibrant community where ideas flourish and connections thrive.

One of Our Valued Members

Thank you for being part of our community. Your presence enriches our shared experiences. Let's continue this journey together!

Home php codes find ip address location .?

find ip address location .?

0

Welcome to shortlearner.com, in this post we will see how to find the IP address, city, browser and country of the
Visitor of your website. Before starting this tutorial I suggest you put the code into a PHP file and call this PHP file into the header file
On your website, it will help you to track the activities of user on your website.

First of all we need a database table where we store all the activity of the visitor on our website.

CREATE TABLE visitor_details(
     id MEDIUMINT NOT NULL AUTO_INCREMENT,
     ip VARCHAR(30) NOT NULL,
  current_page VARCHAR(30) NOT NULL,
  time VARCHAR(30) NOT NULL,
user_agent VARCHAR(30) NOT NULL,
  country VARCHAR(30) NOT NULL,
  city VARCHAR(30) NOT NULL,
     PRIMARY KEY (id)
);

After creating table, we write a PHP script that will help us to find all the information Of visitor.


In the below code we will geoplugin.net library to find the country and the city of the visitor.
Just remember one thing, the script will not work in your localhost.

geo.php

<html>
<body>
<div id="wrapper">
<div id="detail_div">
<?php
 $ipaddress = $_SERVER['REMOTE_ADDR'];
 $page = "http://".$_SERVER['HTTP_HOST']."".$_SERVER['PHP_SELF'];
 $datetime = date("F j, Y, g:i a");
 $useragent = $_SERVER['HTTP_USER_AGENT'];
$ip_address=$_SERVER['REMOTE_ADDR'];
 /*Get user ip address details with geoplugin.net*/
$geopluginURL='http://www.geoplugin.net/php.gp?ip='.$ip_address;
$addrDetailsArr = unserialize(file_get_contents($geopluginURL));
 /*Get City name by return array*/
$city = $addrDetailsArr['geoplugin_city'];
 /*Get Country name by return array*/
$country = $addrDetailsArr['geoplugin_countryName'];
 $con = mysqli_connect("localhost","root","password123","shortlearner");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
  $qry="insert into visitor_details (`ip`,`current_page`,`time`,`user_agent`,`country`,`city`) values('$ipaddress','$page','$datetime','$useragent','$country','$city');";
  mysqli_query($con,$qry);
 ?>
</div>
</div>
</body>
</html>

whois.php

in this page, we fetch all the information of visitors which are stored in our database.

<?php
    $url1=$_SERVER['REQUEST_URI'];
    header("Refresh: 10; URL=$url1");
?>

<!DOCTYPE html>
<html>
<head>
<title>Shortlearner </title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>

<body>

<style>
.ch
{
text-align:center;
color:red;
}
</style>
<h1 class="ch">WEBSITE VIEWERS DETAILS</h1>




<div class="jumbotron">
<div class="container">

<form method="post">
<input type="submit" class="btn btn-danger" name="delete" value="DELETE">
<table class="table table-striped table-dark">
  <thead>
    <tr>
     <th>ID</th>
     <th>IP</th>
      <th>CURRENT PAGE</th>
      <th>TIME</th>
<th>USER AGENT</th>
<th>CITY</th>
<th>COUNTRY</th>
      <th><input type="checkbox" id="checkAl">Select All</th>
    </tr>
  </thead>
  <tbody>
<?php 

$con = mysqli_connect("localhost","root","password123","shortlearner");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
$qry= "SELECT * FROM visitor_details ORDER BY ID DESC;";
$result= mysqli_query($con,$qry);
while($row=mysqli_fetch_array($result))
{

?>
    <tr style="background-color:black;">
     
      <td><span style="color:white;"><?php echo $row['ID']; ?></span></td>
<td><span style="color:white;"><?php echo $row['ip']; ?></span></td>
<td><span style="color:white;"><?php echo $row['current_page']; ?></span></td>
<td><span style="color:white;"><?php echo $row['time']; ?></span></td>
<td><span style="color:white;"><?php echo $row['user_agent']; ?></span></td>
<td><span style="color:white;"><?php echo $row['city']; ?></span></td>
<td><span style="color:white;"><?php echo $row['country']; ?></span></td>

      <td><input type="checkbox" id="checkItem" name="check[]" value="<?php echo $row['ID']; ?>"></td>
    </tr>
 <?php
}
?>  
  </tbody>
</table>
<input type="submit" class="btn btn-danger" name="delete" value="DELETE">
</form>
<script>$("#checkAl").click(function () {$('input:checkbox').not(this).prop('checked', this.checked);});</script>
</div>
</div>
<div class="container">
<div class="col-md-12">
<div class="row">

</div>
</div>
</div>

<?php 
if(isset($_POST['delete']))
{
$checkbox = $_POST['check'];
for($i=0;$i<count($checkbox);$i++){
$del_id = $checkbox[$i]; 
mysqli_query($con,"DELETE FROM visitor_details  WHERE ID='".$del_id."'");

}
header("location:whois.php");
}

?>