Login Redirection

This forum contains some common phpBB3 problems and solutions as well as a selection of useful tutorials.

Moderators: Moderator Team, phpBB Moderators

Forum rules/notes
You use any of the fixes listed in this forum at your own risk. You should make full backups before applying any changes to your board. This site is not affiliated with phpBB.com and as such the phpBB team can and will not be held accountable for any issues which arise from using any of the fixes in this forum. Some of the topics here are clones of articles taken from the phpBB.com knowledge base.

Login Redirection

Postby Jackal » 04 Jun 2007 18:16

If you have a login on your site outside of phpBB like we have here on http://www.ktuk.net you will probably want the forum to redirect you back to the sites index rather than the forum index on login or logout. The following code changes are what you need to make...

Open includes/functions.php



Find:


Code: Select all
    // The result parameter is always an array, holding the relevant information...
        
if ($result['status'] == LOGIN_SUCCESS)
        {
            
$redirect request_var('redirect'"{$phpbb_root_path}index.$phpEx");
            
$message = ($l_success) ? $l_success $user->lang['LOGIN_REDIRECT'];
            
$l_redirect = ($admin) ? $user->lang['PROCEED_TO_ACP'] : (($redirect === "{$phpbb_root_path}index.$phpEx" || $redirect === "index.$phpEx") ? $user->lang['RETURN_INDEX'] : $user->lang['RETURN_PAGE']);  


Replace With:



Code: Select all
// The result parameter is always an array, holding the relevant information...

        
if ($result['status'] == LOGIN_SUCCESS)

        {

            
$redirect request_var('redirect'"../index.php"); //this is the url you want the redirection to take you.

            
$message = ($l_success) ? $l_success $user->lang['LOGIN_REDIRECT'];

            
$l_redirect = ($admin) ? $user->lang['PROCEED_TO_ACP'] : (($redirect === "{$phpbb_root_path}index.$phpEx") ? $user->lang['RETURN_INDEX'] : $user->lang['RETURN_PAGE']);  




Open ucp.php

Find:

Code: Select all
case 'login':
        if (
$user->data['is_registered'])
        {
            
redirect(append_sid("{$phpbb_root_path}index.$phpEx"));
        }

        
login_box(request_var('redirect'"index.$phpEx"));
    break;  


Replace With:

Code: Select all
case 'login':
        if (
$user->data['is_registered'])
        {
            
redirect(append_sid("../index.php")); //this is the url you want the redirection to take you.
        
}

        
login_box(request_var('redirect'"../index.php")); //this is the url you want the redirection to take you.
    
break;  


Find:

Code: Select all
case 'logout':
        if (
$user->data['user_id'] != ANONYMOUS && (!empty($_GET['sid']) && ($_GET['sid'] == $user->session_id)))
        {
            
$user->session_kill();
            
$user->session_begin();
            
$message $user->lang['LOGOUT_REDIRECT'];
        }
        else
        {
            
$message = ($user->data['user_id'] == ANONYMOUS) ? $user->lang['LOGOUT_REDIRECT'] : $user->lang['LOGOUT_FAILED'];
        }
        
meta_refresh(3append_sid("{$phpbb_root_path}index.$phpEx"));
    
        
$message $message '<br /><br />' sprintf($user->lang['RETURN_INDEX'], '<a href="' append_sid("{$phpbb_root_path}index.$phpEx") . '">''</a> ');
        
trigger_error($message);  


Replace with:



Code: Select all
case 'logout':

        if (
$user->data['user_id'] != ANONYMOUS)

        {

            
$user->session_kill();

            
$user->session_begin();

        }



        
meta_refresh(3append_sid("../index.php")); //this is the url you want the redirection

        
$message $user->lang['LOGOUT_REDIRECT'] . '<br /><br />' sprintf($user->lang['RETURN_INDEX'], '<a href="' append_sid("../index.php") . '">''</a> ');

        
trigger_error($message);  
Image







//firefox
User avatar
Jackal
Evil Admin
Evil Admin
 
Posts: 46,465
Joined: 22 Oct 2004 17:34
Location: Hereford UK
Blog: View Blog (11)
Gender: Male

Login Redirection

Sponsor

Sponsor
 

Re: Login Redirection

Postby Jackal » 04 Jun 2007 18:35

The above is only useful if you can use a login from outside the forum and your web pages use the phpBB sessions management.

It is worth noting that for session integration to work outside of the forum the forums cookie domain will need changing to /

All pages you wish to use the phpBB3 session management must have have at least this in the top of the page,
Code: Select all
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './phpBB3/';//relative path to phpBB3 forum directory
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include(
$phpbb_root_path . 'common.' . $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
// Language file (see documentation related to language files)
$user->setup('mypage'); 


Login Form - as would be output using print or echo in a php script
Code: Select all
<form action=\"". append_sid("{$phpbb_root_path}ucp.php") . "\" method=\"post\">
<input type="hidden" name="mode" value="login" />
User Name:<input type="text"  name="username" />
Password:<input type="password" name="password" />
Hidden:<input type="checkbox" class="radio" name="viewonline" />
<input type="hidden" name="autologin" value="1" />
<input type="submit" value="Submit" name="login" />
</form>
Also, if you do not want to hard code the redirection as shown in the first post in this topic you should be able to send the redirection as a hidden value in the login form by adding
Code: Select all
<input type="hidden" name="redirect" value="./index.php">
replacing the value with your desired redirection.

Log Out Button
Code: Select all
<form action=\"". append_sid("{$phpbb_root_path}ucp.php") . "\" method=\"post\">
<input type="hidden" name="mode" value="logout" />
Logout: <input type="submit" value="Submit" name="logout" />
</form>


All links on external pages will need to use the append_sid() function to add the session id.
Last edited by Jackal on 29 Sep 2008 18:15, edited 2 times in total.
Reason: Edited to include new phpbb_root path format.
Image







//firefox
User avatar
Jackal
Evil Admin
Evil Admin
 
Posts: 46,465
Joined: 22 Oct 2004 17:34
Location: Hereford UK
Blog: View Blog (11)
Gender: Male

Re: Login Redirection

Postby RobinSports.com » 19 Jun 2007 20:36

I have a couple of questions that I would ask your assistance on. I have loaded the phpBB3 onto my site and would like to add user levels to the constants file as follows.

define("ADMIN_NAME", "admin");
define("GUEST_NAME", "Guest");
define("ADMIN_LEVEL", 9);
define("ROBIN_LEVEL", 3);
define("USER_LEVEL", 1);
define("GUEST_LEVEL", 0);

The goal of this is to restrict certain portions of the site to users which have the appropriate level.
Is it possible to add the constants, create a new dataset in the DB table 'phpBB3_users' called user_id, and call to the following function in session without screwing the whole system up:

}

* isAdmin - Returns true if currently logged in user is
* an administrator, false otherwise.
*/
function isAdmin(){
return ($this->userlevel == ADMIN_LEVEL ||
$this->username == ADMIN_NAME);
}
}

* isGuest - Returns true if currently logged in user is
* a guest, false otherwise.
*/
function isGuest(){
return ($this->userlevel == GUEST_LEVEL ||
$this->username == GUEST_NAME);
}
}

* isRobin - Returns true if currently logged in user is
* a robin user, false otherwise.
*/
function isRobin(){
return ($this->userlevel == ROBIN_LEVEL ||
$this->username == ROBIN_NAME);
}
}

* isMember - Returns true if currently logged in user is
* a regular member, false otherwise.
*/
function isMember(){
return ($this->userlevel == MEMBER_LEVEL ||
$this->username == MEMBER_NAME);
}

So new page would have session variables for phpBB3 login at the head followed by if $_session==isAdmin.... etc etc.

Sorry. This is a lot to look at I know. I would really appreciate any input you could give as my programming skills are novice to intermediate at best. (only learned HTML and PHP 3 months ago)

Thank you.
RobinSports.com
Newbie Nutter
Newbie Nutter
 
Posts: 3
Joined: 19 Jun 2007 20:14
Gender: Male

Re: Login Redirection

Postby RobinSports.com » 19 Jun 2007 21:14

As a side note, would it be easier to use the 'usergroups' function set up already in phpBB3 to restrict access to certain webpages? is that possible?

Thank you again.
RobinSports.com
Newbie Nutter
Newbie Nutter
 
Posts: 3
Joined: 19 Jun 2007 20:14
Gender: Male

Re: Login Redirection

Postby Jackal » 20 Jun 2007 05:26

The goal of this is to restrict certain portions of the site to users which have the appropriate level.
Is it possible to add the constants, create a new dataset in the DB table 'phpBB3_users' called user_id, and call to the following function in session without screwing the whole system up:


That is very possible :) you would have to call the column something other than user_id as that column already exists, but yes you could just have a column which contains numbers which match constants. This data would be pulled into the user data and could then be checked against as your example show. Using groups would be the be a better way as you could then easily assign members permissions to view or not to view pages by adding or removing them from groups. However group info is not automatically pulled into a data array like the user data so you would have to write some custom code to get the groups the user is in and then check against them.
Image







//firefox
User avatar
Jackal
Evil Admin
Evil Admin
 
Posts: 46,465
Joined: 22 Oct 2004 17:34
Location: Hereford UK
Blog: View Blog (11)
Gender: Male

Re: Login Redirection

Postby RobinSports.com » 20 Jun 2007 17:28

Thank you Jackal! //clap I will be working on this. I hope you don't mind if I ask further questions.
RobinSports.com
Newbie Nutter
Newbie Nutter
 
Posts: 3
Joined: 19 Jun 2007 20:14
Gender: Male

Re: Login Redirection

Postby Guest » 20 Jun 2007 20:47

Jackal wrote:The above is only useful if you can use a login from outside the forum and your web pages use the phpBB sessions management.

All pages you wish to use the phpBB3 session management must have have at least this in the top of the page,
Code: Select all
define('IN_PHPBB', true);
$phpbb_root_path = 'phpBB3/';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);

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


Login Form
Code: Select all
<form action="./phpBB3/ucp.php" method="post">
<input type="hidden" name="mode" value="login" />
User Name:<input type="text"  name="username" />
Password:<input type="password" name="password" />
Hidden:<input type="checkbox" class="radio" name="viewonline" />
<input type="hidden" name="autologin" value="1" />
<input type="submit" value="Submit" name="login" />
</form>
Also, if you do not want to hard code the redirection as shown in the first post in this topic you should be able to send the redirection as a hidden value in the login form by adding
Code: Select all
<input type="hidden" name="redirect" value="./index.php">
replacing the value with your desired redirection.

Log Out Button
Code: Select all
<form action="./phpBB3/ucp.php" method="post">
<input type="hidden" name="mode" value="logout" />
Logout: <input type="submit" value="Submit" name="logout" />
</form>


Hi guys a quick question on jackles post, ive used the same script and it works however if i then name the login form and use a JS submit it will take me to the login page but not actually log me in any ideas on why?
User avatar
Guest
 

Re: Login Redirection

Postby Panaflakin » 21 Jun 2007 06:13

How do i make so the login box will display only if the user is not logged in the forums, and then to display her username, avatar, and logout link if the user is logged?
User avatar
Panaflakin
 

Re: Login Redirection

Postby Jackal » 21 Jun 2007 06:27

RobinSports.com wrote:Thank you Jackal! //clap I will be working on this. I hope you don't mind if I ask further questions.

Feel free to ask any questions you wish :)



Anonymous wrote:
Jackal wrote:The above is only useful if you can use a login from outside the forum and your web pages use the phpBB sessions management.

All pages you wish to use the phpBB3 session management must have have at least this in the top of the page,
Code: Select all
define('IN_PHPBB', true);
$phpbb_root_path = 'phpBB3/';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);

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


Login Form
Code: Select all
<form action="./phpBB3/ucp.php" method="post">
<input type="hidden" name="mode" value="login" />
User Name:<input type="text"  name="username" />
Password:<input type="password" name="password" />
Hidden:<input type="checkbox" class="radio" name="viewonline" />
<input type="hidden" name="autologin" value="1" />
<input type="submit" value="Submit" name="login" />
</form>
Also, if you do not want to hard code the redirection as shown in the first post in this topic you should be able to send the redirection as a hidden value in the login form by adding
Code: Select all
<input type="hidden" name="redirect" value="./index.php">
replacing the value with your desired redirection.

Log Out Button
Code: Select all
<form action="./phpBB3/ucp.php" method="post">
<input type="hidden" name="mode" value="logout" />
Logout: <input type="submit" value="Submit" name="logout" />
</form>


Hi guys a quick question on jackles post, ive used the same script and it works however if i then name the login form and use a JS submit it will take me to the login page but not actually log me in any ideas on why?

surely a javascript submit would replace the login form and thus should submit straight the php script which processes the login.




Anonymous wrote:How do i make so the login box will display only if the user is not logged in the forums, and then to display her username, avatar, and logout link if the user is logged?


That is simple

you can either use

Code: Select all
if ($user->data['is_registered'])
{
logout button
}
else
{
login box
}


or
Code: Select all
if ($user->data['user_id'] == ANONYMOUS)
{
login box
}
else
{
logout button
}



to show the avatar you will need to include the includes/functions_display.php page and then echo
Code: Select all
get_user_avatar($user->data['user_avatar'], $user->data['user_avatar_type'], $user->data['user_avatar_width'], $user->data['user_avatar_height']);


displaying the username is as easy as
Code: Select all
echo $user->data['username'];


Obviously if you want to use the phpBB templating system on the pages you are creating you will have to follow the phpBB3's method of assigning creating a template file to go with your php page and then properly assigning template variables.
Image







//firefox
User avatar
Jackal
Evil Admin
Evil Admin
 
Posts: 46,465
Joined: 22 Oct 2004 17:34
Location: Hereford UK
Blog: View Blog (11)
Gender: Male

Re: Login Redirection

Postby blur » 22 Jun 2007 09:39

Hi Jackal

At one point I found a thread on this site which showed how to set-up the remote login to the forums from a index page like on ktuk.net. I searched and searched and haven't been able to find that mod.
blur
Newbie Nutter
Newbie Nutter
 
Posts: 1
Joined: 14 Jun 2007 08:47
Gender: Male

Re: Login Redirection

Postby Jackal » 22 Jun 2007 09:42

blur wrote:Hi Jackal

At one point I found a thread on this site which showed how to set-up the remote login to the forums from a index page like on ktuk.net. I searched and searched and haven't been able to find that mod.



Assuming you are using phpBB3 then the first two posts in this topic cover it, the second post is the one which covers the login and session management.
Image







//firefox
User avatar
Jackal
Evil Admin
Evil Admin
 
Posts: 46,465
Joined: 22 Oct 2004 17:34
Location: Hereford UK
Blog: View Blog (11)
Gender: Male

Re: Login Redirection

Postby Shifty » 28 Jun 2007 06:16

Im trying to get a Login box to appear on my website, at the top, which displays as a "Your CP" "Messages" etc if the user is logged in. But Im having major trouble trying to piece together what code to put at the top to call phpbb3 functions first of all, and secondly I'm not quite sure how to get the login box to display if guest, "else if" the UCP or Messages links are displayed.

I appreciate you posting this to get me on the right track, but perhaps a little clarification. :D
Shifty
Newbie Nutter
Newbie Nutter
 
Posts: 9
Joined: 28 Jun 2007 06:11
Gender: Male

Re: Login Redirection

Postby Jackal » 28 Jun 2007 06:20

Shifty wrote:Im trying to get a Login box to appear on my website, at the top, which displays as a "Your CP" "Messages" etc if the user is logged in. But Im having major trouble trying to piece together what code to put at the top to call phpbb3 functions first of all, and secondly I'm not quite sure how to get the login box to display if guest, "else if" the UCP or Messages links are displayed.

I appreciate you posting this to get me on the right track, but perhaps a little clarification. :D


The basics to using session management and a basic login box are included in this topic, however what else you do is limited only by your knowledge of html and php. When you say
at the top, which displays as a "Your CP" "Messages"
can you explain what you mean a little clearer :)
Image







//firefox
User avatar
Jackal
Evil Admin
Evil Admin
 
Posts: 46,465
Joined: 22 Oct 2004 17:34
Location: Hereford UK
Blog: View Blog (11)
Gender: Male

Re: Login Redirection

Postby Shifty » 28 Jun 2007 06:28

Well I put this at the top of my page (right under the DOCTYPE line)

Code: Select all
<?php
define('IN_PHPBB', true);
$phpbb_root_path = 'phpBB3/';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);

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


Then I put this where I'd like the box to display:

Code: Select all
<?php
if ($user->data['user_id'] == ANONYMOUS)
{
login box
}
else
{
logout button
}
?>


It doesnt even load the page. Just blankness. Now Im sure I'm doing something wrong, but Im just so confused. Im not a complete idiot when it comes to HTML or PHP (although Im just now learning the latter). Okay Ill explain what I want to do more clearly:

At the top of my page [spam removed - karlsemple] I want to have a line that reads like this if a guest visitor: "Welcome Guest - Login | Register" and if the user is logged in I want it to read like this: "Welcome <Username> - Control Panel | No New Messages"
Last edited by Jackal on 02 Jul 2007 20:27, edited 1 time in total.
Reason: No spam please
Shifty
Newbie Nutter
Newbie Nutter
 
Posts: 9
Joined: 28 Jun 2007 06:11
Gender: Male

Re: Login Redirection

Postby Jackal » 28 Jun 2007 06:51

Code: Select all
  <?php
if ($user->data['user_id'] == ANONYMOUS)
{
login box
}
else
{
 logout button
}
?>


Is just an example, the 'login box' and 'logout button' is where you place the code which generates the code for the items.
Image







//firefox
User avatar
Jackal
Evil Admin
Evil Admin
 
Posts: 46,465
Joined: 22 Oct 2004 17:34
Location: Hereford UK
Blog: View Blog (11)
Gender: Male

Next

Return to Common phpBB3 problems and tutorials

Who is online

Users browsing this forum: CommonCrawl [Bot] and 4 guests