How can the code in the provided PHP script be improved to prevent unauthorized access?
The issue with the provided PHP script is that it does not have any form of authentication or authorization mechanism, allowing anyone to access the protected content. To prevent unauthorized access, you can implement a simple login system where users need to enter a username and password to access the content.
<?php
session_start();
// Check if the user is logged in
if (!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] !== true) {
header('Location: login.php');
exit;
}
// Protected content here
echo "Welcome to the protected area!";
// Logout button
echo '<form action="logout.php" method="post">
<input type="submit" value="Logout">
</form>';
?>