How to tell if a user is logged in to a Drupal site

In Drupal, data for the current user is stored in the global variable $user. To determine if the user is logged in, use the following code:

<?php
 
global $user;
  if (
$user->uid) {
   
// User is logged in.
 
}
?>

This works because all guests browsing the site will have a uid of 0. That will return FALSE in the if statement while any positive number will return TRUE. In Drupal, logged in users are referred to as authenticated users.