What are the potential pitfalls of using global variables like $SESSION in PHP scripts and how can they affect the overall functionality and security of the application?

Using global variables like $SESSION in PHP scripts can lead to security vulnerabilities such as session hijacking, session fixation, and session data tampering. To mitigate these risks, it is recommended to use PHP's built-in session handling functions to manage session data securely.

session_start();
// Use $_SESSION superglobal to store and access session data securely
$_SESSION['user_id'] = 123;