How can PHP be used to restrict access to certain pages based on user registration and login status?

To restrict access to certain pages based on user registration and login status in PHP, you can implement a session-based authentication system. When a user logs in, set a session variable to indicate their logged-in status. On restricted pages, check if this session variable is set to allow access. If not, redirect the user to a login page.

session_start();

// Check if user is logged in
if(!isset($_SESSION['logged_in'])) {
    header("Location: login.php");
    exit();
}

// Restricted page content here