简体   繁体   中英

How to create a button to close active session in joomla 2.5

I need to create a button to log out in Joomla, I mean, the user usually enter the session but then must press the button I created to close this session, I know how to check if any user has entered the session and I know how to display the button, what I don't know is how to make that button close the actual user session (log out).

This is the base code I have:

<?php $user =& JFactory::getUser(); ?>
<?php if ( ($user->id)==0 ) : ?>

    //***code for not opened session***

<?php else : ?>

    <form id="form1" name="form1" method="post" action="">
      <input type="button" name="button" id="button" value="Close Session" />
    </form>

<?php endif ?>

How to make that button to close the joomla 2.5 session, I checked the API page but i didn't find it.

To create a log out button, you could create a link with the class "button".

In Joomla > 1.7 (also 2.5.x) you need the JUtility::getToken() part to make the login successful: Use the optional "return" part for redirecting the user back to the page where the user was when clicking the button

<a class="button" href="<?php echo JRoute::_('index.php?option=com_users&task=user.logout&'. JUtility::getToken().'=1'); ?>">
    Logout
</a>

If you want to redirect back to the page where the user was when he/she clicked the logout button, add a base64 encoded return parameter:

<a class="button" href="<?php echo JRoute::_('index.php?option=com_users&task=user.logout&'. JUtility::getToken().'=1&return='.base64_encode(JURI::current())); ?>">
    Logout
</a>

实际上是正确的链接

JRoute::_('index.php?option=com_users&task=user.logout&'. JUtility::getToken() .'=1');

This also works for joomla 2.5. I swapped JURI::current() with $_SERVER['REQUEST_URI'] from @Beatniak code

<a class="button" href="<?php echo JRoute::_('index.php?option=com_users&task=user.logout&'. JUtility::getToken().'=1&return='.base64_encode($_SERVER['REQUEST_URI'])); ?>">
    Logout
</a>

u may try also this for Joomla 2.5

<a href="index.php?option=com_users&task=user.logout&<?php echo JUtility::getToken(); ?>=1">
<input  type="button" name="Submit" class="button" value="Logout">
</a>

works good for me

see this link http://codefresh.wordpress.com/2011/10/22/log-out-link-in-joomla/ . you get the url to logout the current user.

With Joomla 2.5 you need to make a new menu item titled Logout and choose the menu type to be the User Manager - Login Form.

By doing this if the user is logged in then this will take them to a page with a logout button instead of the login form.

Also if you set the access permissions to only display this logout menu item for registered users.

This was a very easy solution to do without any coding knkowledge. Really there should be a menu item option for the user manager - logout.

Link to a php page

<a href="/link.php">Sign Out</a>

On the php page, simply end the session, then redirect to a new page.

<?php
    session_destroy();   //session is now over


    header( 'Location: http://www.yoursite.com/welcomepage.php' ) ; //relink to a page, with user logged out

?>

User is now logged out, and redirected to the welcome page

使用此链接 -

http://www.domain.com/index.php?index.php?option=com_user&task=user.logout&token=<?php echo JUtility::getToken(); ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM