What potential pitfalls could lead to the issue of $_SESSION['user'] changing to "root" after refreshing?

The issue of $_SESSION['user'] changing to "root" after refreshing could be caused by a vulnerability in the session handling code, allowing unauthorized users to manipulate the session data. To solve this issue, ensure that session data is properly validated and sanitized before being used to prevent unauthorized changes.

session_start();

// Validate and sanitize the user input before assigning it to the session variable
if(isset($_POST['user']) && is_string($_POST['user'])) {
    $_SESSION['user'] = filter_var($_POST['user'], FILTER_SANITIZE_STRING);
}