What are some alternative methods for user authentication in PHP that do not involve htaccess?

Using alternative methods for user authentication in PHP can be necessary when you want to avoid using .htaccess files for security reasons or if you need more flexibility in your authentication process. One common alternative method is to implement session-based authentication where user credentials are verified and stored in a session variable after successful login. This approach allows you to authenticate users without relying on .htaccess files.

// Start the session
session_start();

// Check if the user is already logged in
if(isset($_SESSION['user_id'])) {
    // User is already authenticated
    // Proceed with the rest of your application
} else {
    // User is not authenticated
    // Redirect to login page
    header("Location: login.php");
    exit();
}