How can PHP be used to create a secure admin-only page for viewing images in a folder?
To create a secure admin-only page for viewing images in a folder, you can use PHP to check if the user is logged in as an admin before displaying the images. You can store the images in a folder outside of the web root to prevent direct access, and use PHP to fetch and display them on the admin page only if the user is authenticated as an admin.
<?php
session_start();
// Check if user is logged in as admin
if(isset($_SESSION['is_admin']) && $_SESSION['is_admin'] == true){
$imageFolder = 'path/to/image/folder/';
// Get list of images in folder
$images = glob($imageFolder . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
// Display images
foreach($images as $image){
echo '<img src="' . $image . '" alt="Image">';
}
} else {
echo 'You do not have permission to view this page.';
}
?>