What are common pitfalls when using PHP scripts for password reset functionalities?

Common pitfalls when using PHP scripts for password reset functionalities include not properly sanitizing user input, not securely storing reset tokens, and not implementing proper expiration times for reset links. To solve these issues, always validate and sanitize user input, use secure hashing algorithms to store reset tokens, and set expiration times for reset links to prevent unauthorized access.

// Example of securely storing reset tokens and implementing expiration time

// Generate a unique token for password reset
$token = bin2hex(random_bytes(16));

// Store the token securely in the database along with the user ID and expiration time
$reset_token = [
    'user_id' => $user_id,
    'token' => password_hash($token, PASSWORD_DEFAULT),
    'expiration_time' => date('Y-m-d H:i:s', strtotime('+1 hour')) // Token expires in 1 hour
];

// Save the reset token in the database
// Example SQL query: INSERT INTO reset_tokens (user_id, token, expiration_time) VALUES (:user_id, :token, :expiration_time)