What are the potential security risks associated with using sessions in PHP for preventing duplicate actions?
Using sessions in PHP for preventing duplicate actions can pose security risks such as session fixation attacks or session hijacking if not implemented properly. To mitigate these risks, it is important to regenerate the session ID after a successful login or before performing any critical actions to ensure that each session is unique and secure.
session_start();
if (isset($_SESSION['logged_in']) && $_SESSION['logged_in'] === true) {
// Regenerate session ID
session_regenerate_id();
// Perform critical actions
} else {
// Redirect to login page
header("Location: login.php");
exit();
}