Skip to content
PHP Logo

How can SQL be utilized to store and manage visitor IP addresses in a database in PHP?

To store and manage visitor IP addresses in a database using SQL in PHP, you can create a table in your database to store the IP addresses along with a timestamp. Whenever a visitor accesses your website, you can retrieve their IP address using PHP and then insert it into the database along with the current timestamp.

php
<?php
// Connect to your database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "your_database";

$conn = new mysqli($servername, $username, $password, $dbname);

// Get visitor's IP address
$visitor_ip = $_SERVER['REMOTE_ADDR'];

// Insert visitor's IP address into the database
$sql = "INSERT INTO visitor_ips (ip_address, visit_time) VALUES ('$visitor_ip', NOW())";
$conn->query($sql);

// Close the database connection
$conn->close();
?>