What are the best practices for handling user activation and verification in PHP applications?

User activation and verification in PHP applications are crucial for ensuring the security and validity of user accounts. One best practice is to generate a unique activation code for each user and send it to their email address for verification. Upon receiving the activation code, the user can verify their account by entering the code on the website.

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

// Save the activation code in the database for the user
// Send the activation code to the user's email address

// Verify the user's account using the activation code
if(isset($_GET['activation_code'])) {
    $activation_code = $_GET['activation_code'];
    
    // Check if the activation code matches the one in the database
    // Activate the user's account if the codes match
}