My email verification is working properly on my localhost but when I hosted it, it is not working. When I clicked the link “Verify Email” on the email the user received, it always shows “Invalid token or expired.” the database connection is same with the username, password, database name and servername on the database I created on the hosting provider. Here is my code:
<?php
include 'db_connection.php';
// Check if token is provided in the URL
if (isset($_GET['token'])) {
$token = $_GET['token'];
// Prepare SQL statement to fetch email associated with the token
$stmt = $connection->prepare("SELECT email FROM email_verification WHERE token = ?");
$stmt->bind_param("s", $token);
$stmt->execute();
$result = $stmt->get_result();
// Check if a row is returned
if ($result->num_rows === 1) {
// Fetch Email
$row = $result->fetch_assoc();
$email = $row['email'];
// Mark the email as verified in the users table
$update_stmt = $connection->prepare("UPDATE users SET verified = 1 WHERE email = ?");
$update_stmt->bind_param("s", $email);
$update_stmt->execute();
// Delete the verification token from the email_verification table
$delete_stmt = $connection->prepare("DELETE FROM email_verification WHERE token = ?");
$delete_stmt->bind_param("s", $token);
$delete_stmt->execute();
// Close statements
$stmt->close();
$update_stmt->close();
$delete_stmt->close();
// Redirect to the Log in page
header("Location: index.php");
exit;
} else {
echo "Invalid token or token expired.";
}
} else {
echo "Token not provided.";
}
// Close database connection
if (isset($connection)) {
$connection->close();
}
New contributor
Roland Ruiz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.