Untitled
unknown
plain_text
a year ago
2.9 kB
11
Indexable
<?php
include '../model/mydb.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Find User by IP Address</title>
<script>
function searchUserByIp() {
var ipAddress = document.getElementById("ip_address").value;
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
document.getElementById("result").innerHTML = xhr.responseText;
}
};
xhr.open("GET", "../control/ajax.php?ip_address=" + ipAddress, true);
xhr.send();
}
</script>
</head>
<body>
<h1>Find User by IP Address</h1>
<label for="ip_address">IP Address:</label>
<input type="text" id="ip_address" name="ip_address">
<button onclick="searchUserByIp()">Search</button>
<h2>Search Result:</h2>
<div id="result"></div>
</body>
</html>
<?php
class MyDB {
private $servername = "localhost";
private $username = "root";
private $password = "";
private $dbname = "task3";
public $conn;
// Create a connection to the database
public function createConObject() {
$this->conn = new mysqli($this->servername, $this->username, $this->password, $this->dbname);
if ($this->conn->connect_error) {
die("Connection failed: " . $this->conn->connect_error);
}
return $this->conn;
}
// Function to find user by IP address
public function findUserByIp($ip_address) {
$sql = "SELECT * FROM mytable WHERE ip_address = ?";
$stmt = $this->conn->prepare($sql);
$stmt->bind_param("s", $ip_address);
$stmt->execute();
return $stmt->get_result();
}
}
?>
<?php
include '../model/mydb.php';
if (isset($_GET['ip_address'])) {
$ip_address = $_GET['ip_address'];
$db = new MyDB();
$conobj = $db->createConObject();
$result = $db->findUserByIp($ip_address);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "<table border='1'>";
echo "<tr><td>First Name:</td><td>" . $row["first_name"] . "</td></tr>";
echo "<tr><td>Last Name:</td><td>" . $row["last_name"] . "</td></tr>";
echo "<tr><td>Email:</td><td>" . $row["email"] . "</td></tr>";
echo "<tr><td>Gender:</td><td>" . $row["gender"] . "</td></tr>";
echo "<tr><td>IP Address:</td><td>" . $row["ip_address"] . "</td></tr>";
echo "</table>";
}
} else {
echo "No user found with the given IP address.";
}
}
?>Editor is loading...
Leave a Comment