How can PHP developers ensure that an IP address is only stored once per day in a database table like "blog_visitors"?

To ensure that an IP address is only stored once per day in a database table like "blog_visitors", PHP developers can first check if an entry for that IP address already exists for the current date before inserting a new record. This can be achieved by querying the database to see if there is a record with the same IP address and date. If a record is found, the new entry should not be inserted.

// Connect to the database
$pdo = new PDO('mysql:host=localhost;dbname=your_database', 'username', 'password');

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

// Check if a record for this IP address and today's date already exists
$stmt = $pdo->prepare("SELECT COUNT(*) FROM blog_visitors WHERE ip_address = ? AND visit_date = CURDATE()");
$stmt->execute([$ip_address]);
$count = $stmt->fetchColumn();

if($count == 0) {
    // Insert a new record for the IP address
    $stmt = $pdo->prepare("INSERT INTO blog_visitors (ip_address, visit_date) VALUES (?, CURDATE())");
    $stmt->execute([$ip_address]);
}