Show a Form Only to Logged-In Users

Sometimes you only want logged-in users to see a form — for example, when collecting private data or offering something exclusive. In this guide, we’ll walk you through how to hide a form from guests and show a helpful message instead, including a link to the login page.

Hide the Form From Guests

To hide the form from guests, edit your form, go to Behavior > PHP Scripts > Form Display, and add:

if ($user->guest)
{
     $formLayout = '';
}

To inform your guests that this form is only accessible to logged-in users, you can display a helpful message that includes a login link, notifying them that they must log in first.

To add this helpful message, edit your form, go to Behavior > PHP Scripts > Form Display, and add:

if ($user->guest)
{
     $loginUrl = \Joomla\CMS\Router\Route::_('index.php?option=com_users&view=login');
     $formLayout = sprintf('You must <a href="/%s">log in</a> first before you can view this form.', $loginUrl);
}
Last updated on Jun 3rd 2025 17:06