4

I want to create a button that allows me to disconnect the user, i have searched and found that the module of type "connexion" is what i need, but it doesnt work for me, it gives me an error.

so what i want to know is how can i create a button with php/html that disconnect the user? enter image description here

Thank you

  • Why not simply use Joomla's Login module? When the user is logged in, it will provide a button allowing them to logout – Lodder Apr 11 '16 at 17:03
  • It doesnt work for me, i clic create and it keeps loading and loading with no result. – zenami Apr 11 '16 at 17:07
8

You can create a simple logout-link like this:

<?php 
    $user = JFactory::getUser();
    $userToken = JSession::getFormToken();
    if (!$user->guest) : ?>
        <a href="index.php?option=com_users&task=user.logout&<?php echo $userToken; ?>=1" class="btn">Log out</a>
<?php endif; ?>

The Log out button will only be visible to logged-in users, and when clicked the user will be logged out instantly. If you also want to show a Login button, you can modify the PHP if/else statement and display a link to the login page (index.php?option=com_users&view=login) for guests.

<?php 
    $user = JFactory::getUser();
    $userToken = JSession::getFormToken();
    if ($user->guest) : ?>
        <a href="index.php?option=com_users&view=login" class="btn">Log in</a>
<?php else: ?>
        <a href="index.php?option=com_users&task=user.logout&<?php echo $userToken; ?>=1" class="btn">Log out</a>
<?php endif; ?>

The combination of PHP and HTML is necessary because we need to get the user token, without it the link will fail.

Usage:

The code can be used in several ways:

  1. Download from GitHub
    This module is available on GitHub as an installable Joomla module: https://github.com/Joomla-StackExchange/loginButton

  2. Custom HTML
    Some Custom HTML modules accept PHP code (the one included in Joomla by default does not). I recommend Custom HTML Advanced. Just paste the code and publish the module wherever you want the button to show up. Remember you can also place a module anywhere using {loadposition YourModulePosition}.

  3. Sourcerer
    You can add PHP code anywhere using NoNumbers Sourcerer.

    {source}
    <?php 
        $user = JFactory::getUser();
        $userToken = JSession::getFormToken();
        if (!$user->guest) : ?>
            <a href="index.php?option=com_users&task=user.logout&<?php echo $userToken; ?>=1" class="btn">Log out</a>
    <?php endif; ?>
    {/source}
    
  4. Build a module
    You can build your own module, it's easier than it sounds. A basic tutorial can be found here.

  5. Add to your template
    You can also paste the code directly into your Joomla template, usually ROOT\templates\YOURTEMPLATE\index.php. This is the least flexible solution, but might be a quick solution depending on how your template is configured.

  • Where do i put this code? is there any way to combine it with a html code? – zenami Apr 12 '16 at 8:32
  • @zenami please see my updated answer :) – johanpw Apr 12 '16 at 12:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.