Date Published: February 5, 2025
Is there a Drupal function to check if the user is logged in?
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:
The function is a boolean that returns TRUE if the user is logged in, and FALSE if the user is anonymous.
Note
Functions and their usage can change between Drupal versions.
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();
Did not find what you were looking for?
If this content did not answer your questions, try searching or contacting our support team for further assistance.