What best practices should be followed when creating a registration form with email activation to prevent unauthorized access?

To prevent unauthorized access when creating a registration form with email activation, it is important to validate user input, use secure password hashing techniques, and implement a secure email activation process. This can include generating a unique activation code for each user, sending a verification email with a link to activate the account, and verifying the activation code before allowing access to the account.

// Generate a unique activation code
$activation_code = md5(uniqid(rand(), true));

// Save the activation code in the database along with user details

// Send a verification email to the user with the activation link
$to = $user_email;
$subject = 'Activate your account';
$message = 'Click the following link to activate your account: http://example.com/activate.php?code=' . $activation_code;
$headers = 'From: admin@example.com';
mail($to, $subject, $message, $headers);

// Verify the activation code when the user clicks on the activation link
if(isset($_GET['code'])){
    $activation_code = $_GET['code'];
    
    // Check if the activation code exists in the database
    // If it does, activate the user's account
}