Untitled

mail@pastecode.io avatar
unknown
plain_text
18 days ago
1.3 kB
3
Indexable
Never
<?php
// Step 1: Create a database connection
$host = 'localhost'; // Database host
$user = 'your_username'; // Database username
$pass = 'your_password'; // Database password
$dbname = 'your_database_name'; // Database name

// Create a MySQLi connection
$conn = new mysqli($host, $user, $pass, $dbname);

// Check the connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Step 2: Capture the IP address from the AJAX request
$ip_address = $_POST['ip_address'];  // Make sure this is passed from the AJAX request

// Step 3: Define the SQL query to find the user by IP address
$sql = "SELECT * FROM users WHERE ip_address = '$ip_address'";

$result = $conn->query($sql);

// Step 4: Check if the user exists
if ($result->num_rows > 0) {
    // Initialize an array to store the user data
    $data = array();

    // Fetch the user data
    while ($row = $result->fetch_assoc()) {
        $data[] = $row;
    }

    // Step 5: Convert the data to JSON format
    $json_data = json_encode($data);

    // Output the JSON data
    header('Content-Type: application/json');
    echo $json_data;

} else {
    // If no user found, return a "No data found" message in JSON format
    echo json_encode(array("message" => "No data found"));
}

// Step 6: Close the database connection
$conn->close();
?>
Leave a Comment