How can the use of session_register() in PHP be replaced with more secure and modern methods for storing user session data?

Using session_register() in PHP is considered insecure and deprecated. Instead, you should use the $_SESSION superglobal array to store and retrieve session data securely. This modern approach ensures better security and compatibility with newer PHP versions.

// Start the session
session_start();

// Store data in the session
$_SESSION['user_id'] = 123;
$_SESSION['username'] = 'example';

// Retrieve data from the session
$user_id = $_SESSION['user_id'];
$username = $_SESSION['username'];

// Unset specific session data
unset($_SESSION['user_id']);

// Destroy the session
session_destroy();