Skip to content
PHP Logo

How can sessions be used for server security instead of .htaccess and PHP scripts?

Sessions can be used for server security by storing sensitive information in the session variables instead of passing them through URLs or forms. This helps prevent unauthorized access to sensitive data. Additionally, sessions can be used to track user authentication status and permissions, allowing for more secure access control on the server.

php
<?php
session_start();

// Store sensitive information in session variables
$_SESSION['username'] = 'admin';
$_SESSION['is_admin'] = true;

// Check user authentication status
if(isset($_SESSION['username'])) {
    // User is authenticated
} else {
    // Redirect to login page
    header('Location: login.php');
    exit;
}
?>