What are common pitfalls to avoid when using sessions in PHP for storing CAPTCHA values?

One common pitfall to avoid when using sessions in PHP for storing CAPTCHA values is not properly validating and sanitizing user input before storing it in the session. This can lead to security vulnerabilities such as injection attacks. To solve this issue, always validate and sanitize user input before storing it in the session to ensure data integrity and security.

// Validate and sanitize user input before storing in session
$captcha_value = filter_input(INPUT_POST, 'captcha', FILTER_SANITIZE_STRING);

// Start session
session_start();

// Store validated CAPTCHA value in session
$_SESSION['captcha'] = $captcha_value;