How can multiple pages be protected with a single password input in PHP?
To protect multiple pages with a single password input in PHP, you can create a session variable to store the password once it's entered correctly. Then, on each protected page, check if the session variable is set and matches the correct password. If it does, allow access to the page, otherwise, redirect to a login page.
<?php
session_start();
$correct_password = "password123";
if(isset($_POST['password'])){
if($_POST['password'] == $correct_password){
$_SESSION['authenticated'] = true;
} else {
echo "Incorrect password";
}
}
if(!isset($_SESSION['authenticated']) || $_SESSION['authenticated'] !== true){
header("Location: login.php");
exit();
}
?>
Related Questions
- How can PHP developers optimize their SQL queries to efficiently sort and rank data based on multiple criteria?
- What are some potential challenges or pitfalls to be aware of when working with CSV files in PHP and manipulating their contents?
- What is the purpose of using utf8_encode in PHP and what encoding does it convert from?