What are the best practices for sending confirmation links in auto emails in PHP?

When sending confirmation links in auto emails in PHP, it is essential to ensure the link is unique and secure to prevent unauthorized access. One best practice is to generate a random token for each confirmation link and store it in the database along with the user's email. When the user clicks on the link, verify the token against the database to confirm the user's identity.

// Generate a random token
$token = bin2hex(random_bytes(16));

// Store the token in the database with the user's email
// $userEmail is the user's email address
// $token is the random token generated
// $expiryDate is the expiration date of the token
// Assume $db is the database connection
$stmt = $db->prepare("INSERT INTO confirmation_tokens (email, token, expiry_date) VALUES (?, ?, ?)");
$stmt->execute([$userEmail, $token, $expiryDate]);

// Send the confirmation link in the email
$confirmationLink = "http://example.com/confirm.php?email=$userEmail&token=$token";
// Send the email with the confirmation link to the user