What is the best practice for handling user activation through a PHP script and database interaction?
When handling user activation through a PHP script and database interaction, the best practice is to generate a unique activation code for each user, store it in the database along with the user's information, and send it to the user via email. Upon clicking the activation link in the email, the user's account should be activated in the database.
```php
// Generate a unique activation code
$activationCode = md5(uniqid(rand(), true));
// Store the activation code in the database along with user information
$query = "INSERT INTO users (email, activation_code) VALUES ('$email', '$activationCode')";
$result = mysqli_query($connection, $query);
// Send activation email to the user
$to = $email;
$subject = 'Activate Your Account';
$message = 'Click the following link to activate your account: http://example.com/activate.php?code=' . $activationCode;
$headers = 'From: admin@example.com';
mail($to, $subject, $message, $headers);
```
(Note: This code assumes you have already established a database connection and sanitized user input to prevent SQL injection.)
Related questions
-
form validation error handling user input sanitization
-
UTF-8 encoding data
-
reserved words MySQL PHP scripts