Login / Logout redirect

Looking for a MOD? Have a MOD request? Post here for help. (Note: This forum is community supported; phpBB does not have official MOD authors)
Suggested Hosts
leto2
Registered User
Posts: 5
Joined: Fri Jan 11, 2008 5:24 pm

Login / Logout redirect

Post by leto2 »

I'm trying to integrate phpBB3 with a site I'm building so that I can login and logout in pages outside the forum. So far, I've been able to create the login & logout and carry sessions to pages outside of the forum.

When I login, I include a hidden input in my form to redirect me to a page outside the forum:

Code: Select all

<input type="hidden" name="redirect" value="example.com/mainpage.html">
To logout I use the following link:

Code: Select all

<a href="'.$forumdirectory.'/ucp.php?mode=logout&sid='.$user->session_id.'">Logout</a>
However, the logout always sends me to the forum index. How can I redirect the logout to whichever page I want?

I'm an amateur, so excuse me if this is something easy, but I haven't been able to find the answer.

Thanks,
leto2
Tom Strong
Registered User
Posts: 17
Joined: Wed Oct 17, 2007 2:56 pm

Re: Login / Logout redirect

Post by Tom Strong »

Open ucp.php

On line 96, substitute :
meta_refresh(3, append_sid("{$phpbb_root_path}index.$phpEx"));
With :
meta_refresh(3, append_sid(request_var('redirect', "{$phpbb_root_path}index.$phpEx")));
Works on my PHPBB3 Gold Board. Dont forget to pass the 'redirect' parameter.. :P
kama
Registered User
Posts: 2
Joined: Tue Nov 30, 2004 10:00 am

Re: Login / Logout redirect

Post by kama »

I consider this a bug and this should be in changed in the main code for the next release.

There are probably other places where you might want to redirect to an outside page.

like:

Code: Select all

@@ -60,7 +60,7 @@
        case 'register':
                if ($user->data['is_registered'] || isset($_REQUEST['not_agreed']))
                {
-                       redirect(append_sid("{$phpbb_root_path}index.$phpEx"));
+                       redirect(append_sid(request_var('redirect', "{$phpbb_root_path}index.$phpEx")));
                }
 
                $module->load('ucp', 'register');
schizoman
Registered User
Posts: 2
Joined: Sat Jul 19, 2008 12:21 pm

Re: Login / Logout redirect

Post by schizoman »

Thanks. I, too, was looking for this. I modified it a bit since, once logged out, it isn't necessary for the sid to be in the URL:

Code: Select all

meta_refresh(1, "../index.php");
Now on to custom user fields....
darwing
Registered User
Posts: 13
Joined: Mon Mar 23, 2009 1:50 am

Re: Login / Logout redirect

Post by darwing »

hey guys I want this so bad however as soon as I touch upc I get errors, even if I just change the 3 to a 1!!! what the hell there has to be something im missin
thefa
Registered User
Posts: 15
Joined: Wed Jul 27, 2005 2:43 pm

Re: Login / Logout redirect

Post by thefa »

Hi,

I have tried to get the redirect to work for registration and it seems that what is below is not enough. Am I correct that we need somehow the registration form(s) (s if you are using the warning message to which you need to agree or not) to pass redirect as an hidden form field?

I could not find the way to do that. Could someone tell me if I can get redirect to work for registration and if yes, how?

Many thanks.
kama wrote:I consider this a bug and this should be in changed in the main code for the next release.

There are probably other places where you might want to redirect to an outside page.

like:

Code: Select all

@@ -60,7 +60,7 @@
        case 'register':
                if ($user->data['is_registered'] || isset($_REQUEST['not_agreed']))
                {
-                       redirect(append_sid("{$phpbb_root_path}index.$phpEx"));
+                       redirect(append_sid(request_var('redirect', "{$phpbb_root_path}index.$phpEx")));
                }
 
                $module->load('ucp', 'register');
User avatar
danger89
Registered User
Posts: 14
Joined: Fri Oct 27, 2006 8:17 pm
Name: Melroy van den Berg
Contact:

Re: Login / Logout redirect

Post by danger89 »

About the logout redirect its a little more difficult (because by default it is not possible), however...

File: ucp.php

Line 95 Find:

Code: Select all

meta_refresh(3, append_sid("{$phpbb_root_path}index.$phpEx"));
Replace:

Code: Select all

		
## LOGOUT REDIRECT HACK  -- BEGIN ##
$redirect = request_var('redirect', "{$phpbb_root_path}index.$phpEx");
meta_refresh(3, append_sid($redirect));
Same file...

Line 101 Find:

Code: Select all

		
$message = $message . '<br /><br />' . sprintf($user->lang['RETURN_INDEX'], '<a href="' . append_sid("{$phpbb_root_path}index.$phpEx") . '">', '</a> ');
Replace:

Code: Select all

		
$message = $message . '<br /><br />' . sprintf($user->lang['RETURN_INDEX'], '<a href="' .  append_sid($redirect) . '">', '</a> ');
## LOGOUT REDIRECT HACK  -- END ##
Usage:

Code: Select all

<?php
$forumpath = "forum";

define('IN_PHPBB', true);
$phpbb_root_path = $forumpath."/";
$phpEx = substr(strrchr(__FILE__, '.'), 1);
require_once($phpbb_root_path . 'common.' . $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup();

//example:
echo '<a href="forum/ucp.php?mode=logout&sid='.$user->data['session_id'].'&redirect=../index.php">Logout</a>';
?>
Kind regards,
Melroy van den Berg
Skinny Vinny
Registered User
Posts: 230
Joined: Tue Dec 01, 2009 7:10 pm

Re: Login / Logout redirect

Post by Skinny Vinny »

Old thread brought back up I see.
Well, I'm on a hook kick again tonight.

First, a few notes. Say goodbye to the "you have been logged out click to whatever" screens. This hook will skip them. If you want to keep them, this will not work for you as written (but could easily be modified). To logout with a redirect, simply direct your logout link to any phpbb page (or any site page that has been integrated with $user object) with mode=logout appended to the url. If you do not include a redirect value, that page is what will be displayed to the user as a guest (will have been logged out). To redirect to another page, simply append with &redirect=[address of page] as well.

For example, /phpbb3/index.php?mode=logout&redirect=/someotherpage.php
If from a page outside phpbb, you should append_sid if it's available.

This hook also includes an overwrite of the default logout url of phpbb to the current page. It will target itself. Exceptions of course are posting screen (which will direct to viewtopic or viewforum based on vars present in page url), and ucp and mcp point at index. If you wish to disable this, simply comment out the last line.

This is as close to plug and play as you're going to get.
I'll provide a download link to a ready made file later, but for now you'll need to copy and paste the entire code below into a new php file in includes/hooks. The name must begin with 'hook_'. Example, on my localhost I have this file as:

hook_svmods_logout_anywhere_with_redirect_option.php

Code: Select all

<?php

// Hook by Skinny Vinny of svmods
// Mod request for logout redirection for integrated site pages, etc.

function svmods_logout_anywhere_with_redirect_option(&$hook){
    global $user, $auth;
    if (($user->data['user_id']!=ANONYMOUS) && (!$user->data['is_bot'])){
        $mode=request_var('mode','');
        if ($mode==='logout'){
            $user->session_kill();
            $user->session_begin();
            if ($redirect=request_var('redirect','')){
                header('location: '.append_sid($redirect));
                die();
            }
            $auth->acl($user->data);
            $user->setup();
        }
    }
}

function svmods_logout_url_replacement(&$hook){
    global $user, $template, $phpEx;
    if (($user->data['user_id']!=ANONYMOUS) && (!$user->data['is_bot'])){
        $url=str_replace('&','&',$GLOBALS['HTTP_SERVER_VARS']['REQUEST_URI']);
        $url=str_replace("ucp.$phpEx","index.$phpEx",$url);
        $url=str_replace("mcp.$phpEx","index.$phpEx",$url);
        if (strpos($url,"posting.$phpEx")){
            $forum_id=request_var('f',0);
            if ($topic_id=request_var('t',0)){
                $url="viewtopic.$phpEx?f=$forum_id&t=$topic_id";
            }
            else{
                $url="viewforum.$phpEx?f=$forum_id";
            }
        }
        $delim=(strpos($url,'?') === false) ? '?' : '&';
        $template->_rootref['U_LOGIN_LOGOUT']=append_sid($url.$delim.'mode=logout');
    }
}

$phpbb_hook->register('phpbb_user_session_handler', 'svmods_logout_anywhere_with_redirect_option');
$phpbb_hook->register(array('template','display'), 'svmods_logout_url_replacement'); 
Upload file to the includes/hooks directory, and purge the cache from the administration index.
That's it. Nothing to modify.
Stick it. Purge it. Done.
User avatar
lozu
Registered User
Posts: 59
Joined: Tue Nov 10, 2009 11:07 am
Contact:

Re: Login / Logout redirect

Post by lozu »

I tried

Code: Select all

meta_refresh(3, append_sid(request_var('redirect', "{$phpbb_root_path}index.$phpEx")));
But still redirects to index page no matter what i tried!
ngreen
Registered User
Posts: 15
Joined: Mon Jan 31, 2011 8:57 pm

Re: Login / Logout redirect

Post by ngreen »

I have the same problem as lozu.

danger89's solution worked perfectly for me in the previous version of phpbb but since upgrading to 3.0.8 it no redirects.

Has anyone found a solution for this, or have any idea of how to fix it?
ngreen
Registered User
Posts: 15
Joined: Mon Jan 31, 2011 8:57 pm

Re: Login / Logout redirect

Post by ngreen »

Tracked down the cause of the problem.

http://tracker.phpbb.com/browse/PHPBB3-9912

Just need to replace one line of code (about line 624) in includes/session.php:

if (isset($_GET['sid']))

with

if (isset($_GET['sid']) && $bot)

And my logout hack is working perfectly again :)
Syntax21121
Registered User
Posts: 6
Joined: Wed Mar 23, 2011 5:40 pm

Re: Login / Logout redirect

Post by Syntax21121 »

lozu wrote:I tried

Code: Select all

meta_refresh(3, append_sid(request_var('redirect', "{$phpbb_root_path}index.$phpEx")));
But still redirects to index page no matter what i tried!
try adding ../ in front of the $phpbb_root_path

I just did this and it worked a treat :)
aarondl
Registered User
Posts: 2
Joined: Fri Jun 03, 2011 6:05 pm

Re: Login / Logout redirect

Post by aarondl »

Hi, I was looking through this topic and am wondering, how do you make it so that it redirects back to another page right after a logout instead of going to the "you have successfully logged out" page and then redirecting, I want the same to happen with the login portion too.
User avatar
Tech_Inno
Registered User
Posts: 13
Joined: Tue Jun 28, 2011 3:55 pm

Re: Login / Logout redirect

Post by Tech_Inno »

The method posted by danger89 combined with the one by ngreen seems to work perfectly. Be sure to make backups of any files you change, it's just good practice.

First do this:
danger89 wrote:
File: ucp.php

Line 95 Find:

Code: Select all

meta_refresh(3, append_sid("{$phpbb_root_path}index.$phpEx"));
Replace:

Code: Select all

		
## LOGOUT REDIRECT HACK  -- BEGIN ##
$redirect = request_var('redirect', "{$phpbb_root_path}index.$phpEx");
meta_refresh(3, append_sid($redirect));
Same file...

Line 101 Find:

Code: Select all

		
$message = $message . '<br /><br />' . sprintf($user->lang['RETURN_INDEX'], '<a href="' . append_sid("{$phpbb_root_path}index.$phpEx") . '">', '</a> ');
Replace:

Code: Select all

		
$message = $message . '<br /><br />' . sprintf($user->lang['RETURN_INDEX'], '<a href="' .  append_sid($redirect) . '">', '</a> ');
## LOGOUT REDIRECT HACK  -- END ##
Then do this:
ngreen wrote:
Just need to replace one line of code (about line 624) in includes/session.php:

if (isset($_GET['sid']))

with

if (isset($_GET['sid']) && $bot)
Then you should be good to go!

Example:

Code: Select all

<a title="Log out" href="' . append_sid("{$phpbb_root_path}ucp.$phpEx", 'mode=logout', true, $user->session_id). '&redirect=../test_page.php">Log out</a>
*Note* I'm using version 3.0.8. If you have an earlier version, you may not need to do the step mentioned by ngreen.
agila
Registered User
Posts: 1
Joined: Thu Jul 21, 2005 4:05 am
Contact:

Re: Login / Logout redirect

Post by agila »

Logout redirect to anywhere...

the easiest is to create php redirect and edit ucp.php replace index.$phpEx with your php redirect filename.

example:

Code: Select all

meta_refresh(3, append_sid("{$phpbb_root_path}anyname.$phpEx"));

php redirect code: anyname.php

Code: Select all

<?php
        ob_start();
        echo "Hello.";
        header("Location: http://anywhere.html");
        ob_flush();
    ?>
ECC is my home on web
Locked

Return to “[3.0.x] MOD Requests”