Are there best practices for integrating PHP login functionality with .htaccess restrictions?

When integrating PHP login functionality with .htaccess restrictions, it is important to ensure that users are authenticated by both PHP and .htaccess before granting access to restricted areas of the website. One common approach is to use PHP sessions to track user login status and then set environment variables in .htaccess based on the session status. This allows for seamless integration between PHP login functionality and .htaccess restrictions.

session_start();

if(isset($_SESSION['logged_in']) && $_SESSION['logged_in'] === true){
    $_SERVER['PHP_AUTH_USER'] = $_SESSION['username'];
    $_SERVER['PHP_AUTH_PW'] = $_SESSION['password'];
}