How can a PHP forum determine if a user is logged in when they click on a link?

To determine if a user is logged in when they click on a link in a PHP forum, you can use sessions to track the user's login status. When the user logs in, set a session variable to indicate that they are logged in. Then, on each page where you want to check if the user is logged in, you can verify the session variable to determine their login status.

// Start session
session_start();

// Check if user is logged in
if(isset($_SESSION['logged_in']) && $_SESSION['logged_in'] == true) {
    // User is logged in, allow access
} else {
    // User is not logged in, redirect to login page
    header("Location: login.php");
    exit();
}