Skip to content
PHP Logo

How can a specific directory be protected similar to an intranet using PHP?

To protect a specific directory similar to an intranet using PHP, you can create a PHP script that checks if the user is authenticated before allowing access to the files in that directory. This can be done by using sessions or tokens to authenticate users and restrict access to unauthorized users.

php
<?php
session_start();

// Check if user is authenticated, if not redirect to login page
if(!isset($_SESSION['authenticated']) || $_SESSION['authenticated'] !== true) {
    header('Location: login.php');
    exit;
}

// Code to display files in protected directory goes here
?>