Yes, and the function to use depends on the version of Drupal that you're using with your website.
Drupal 6 and 7
The function to use is called user_is_logged_in
, and it can determine if a given user is logged in. For complete documentation on the function, click the appropriate link from the following, based on your Drupal version:
- Drupal 7
user_is_logged_in
function information from drupal.org - Drupal 6
user_is_logged_in
function information from drupal.org
The function is a boolean that returns TRUE if the user is logged in, and FALSE if the user is anonymous.
Drupal 8
There are a couple of methods that you can use in Drupal 8 to make this check:
- Use the
isAuthenticated()
method on the user. For example, use the following code to determine the state of the current user:$logged_in = \Drupal::currentUser()->isAuthenticated();
- Use
isAnonymous()
to determine if the user is anonymous:\Drupal::currentUser()->isAnonymous();