Verifier email php example

Here is an example of a PHP script that sends a verification email to a user's email address:

<?php

// Configuration
$db_host = 'localhost';
$db_username = 'root';
$db_password = '';
$db_name = 'mydatabase';

// Connect to database
$conn = mysqli_connect($db_host, $db_username, $db_password, $db_name);

// Check connection
if (!$conn) {
    die("Connection failed: ". mysqli_connect_error());
}

// Get user data from form submission
$username = $_POST['username'];
$email = $_POST['email'];

// Check if user already exists
$query = "SELECT * FROM users WHERE email = '$email'";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
    echo "User already exists";
    exit;
}

// Insert user into database
$query = "INSERT INTO users (username, email) VALUES ('$username', '$email')";
mysqli_query($conn, $query);

// Generate verification token
$token = bin2hex(random_bytes(16));

// Store token in database
$query = "UPDATE users SET verification_token = '$token' WHERE email = '$email'";
mysqli_query($conn, $query);

// Send verification email
$subject = "Verify your email address";
$message = "Hello $username,\r\n\r\nPlease click on the following link to verify your email address:\r\n\r\n<a href='http://example.com/verify.php?token=$token'>Verify Email</a>";
$headers = "From: [email protected]\r\nReply-To: [email protected]\r\nContent-Type: text/html; charset=UTF-8";

mail($email, $subject, $message, $headers);

// Close database connection
mysqli_close($conn);

?>

This script assumes that you have a users table in your database with columns for username and email. When a user submits a registration form, this script checks if the user already exists in the database, inserts the user into the database if they don't exist, generates a verification token, stores the token in the database, and sends a verification email to the user's email address.

The verification email contains a link that the user can click on to verify their email address. The link includes the verification token as a query parameter. When the user clicks on the link, you can use the following PHP script to verify the token and update the user's status in the database:

<?php

// Configuration
$db_host = 'localhost';
$db_username = 'root';
$db_password = '';
$db_name = 'mydatabase';

// Connect to database
$conn = mysqli_connect($db_host, $db_username, $db_password, $db_name);

// Check connection
if (!$conn) {
    die("Connection failed: ". mysqli_connect_error());
}

// Get token from URL
$token = $_GET['token'];

// Check if token exists in database
$query = "SELECT * FROM users WHERE verification_token = '$token'";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) == 0) {
    echo "Invalid token";
    exit;
}

// Update user status in database
$query = "UPDATE users SET verified = 1 WHERE verification_token = '$token'";
mysqli_query($conn, $query);

// Close database connection
mysqli_close($conn);

?>

This script checks if the token exists in the database, and if it does, updates the user's status to "verified". You can then use this script to verify the user's email address and allow them to log in to your application.