Dailytip.net

Articles on pretty much anything.

About the author

Author Name is someone.
E-mail me Send mail

Recent posts

Recent comments

Tags

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2009


phpBB 3: How to access user details

This article is a follow up to the article I wrote about integrating the phpBB 3 login with your site.You can find that article here. This time I'd like to take it a little bit further and take a look at what we can do once our user is logged in. I'm going to assume you have sufficient knowledge of PHP to understand the code and, at least, the basics of object orientated programming in PHP. I'm also assuming you have a fully functioning and installed phpBB 3 forum at the point.

To go further into the topic, we'll need to be able to access the data for our user(s). To do this, we have to use the user object and access it's data member which, predictably, holds all the user data. Here's the piece of code we'll need every time we want to use the phpBB user system in our pages. I'll explain the source code after.

 

0:  define('IN_PHPBB', true);
1:  $phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : '/PATH/TO/FORUMS';
2:  $phpEx = substr(strrchr(__FILE__, '.'), 1);
3:  include($phpbb_root_path . 'common.' . $phpEx);
4:  include($phpbb_root_path . 'includes/functions_display.' . $phpEx);
5:  // Start session management
6:  $user->session_begin();
7:  $auth->acl($user->data);

First of all, with this code, you will need to change all the bits in red to the parts specified by that text. Let's look, briefly, at the code now:

  • Line 0
    Defines the "IN_PHPBB" constant that it used for security.
  • Line 1
    Defines the root path constant for including files and doing other things involving the file system.
  • Line 2
    Gets the PHP extension.
  • Line 3
    Includes the "common" file from the forums root directory
  • Line 4
    Includes the display functions file from the forums root directory
  • Line 5
    A comment. Self explanatory
  • Line 6
    Starts the user session
  • Line 7
    The authentication object takes the user data for security stuff

Now that we're able to access the user object ($user in the script) - let's take a look at all the stuff inside of it!

 

0:  foreach ($user->data as $sess => $val)
1:  {
2:   if(!$val )
3:   {
4:   print($sess ."=> NULL");
5:   }
6:   else
7:   {
8:   print($sess ."=>".$val);
9:   }
10:   print("<br />");
11: }

This little bit of code loops through the whole array in $user->data and outputs the keys and their corresponding values in a nice, easy to read fashion. It also replaces empty values with the word "NULL" for easier reading - this is not how it is in the array.

For the main task in this article, I'd like to you to pay special attention to the second to last bit in the array. "is_registered". Login in and out of the admin account and notice how this changes. When a user is logged in, the value is "1". When a user is not logged in, the value is null, or empty. Using this knowledge we can stop guests from viewing certain pages. Here's how we do it.

 

0:  if(!$user->data["is_registered"])
1:  {
2:   die("User is not logged in");
3:  }

This code checks to see if the "is_registered" key in the $user->data array has a value (this is the same as checking to see if the user is logged in or not) and if it doesn't (user isn't logged in) it kills script execution. Using this code we can stop guests from viewing private pages when we're using the phpBB 3 user system.

You can read my article of integrating phpBB3 login here.

Thanks for  reading. I hope my article has helped you in some way. Browse around Daily Tip for more great tips and articles.

Currently rated 5.0 by 13 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by Andy on Wednesday, April 02, 2008 2:51 PM
Permalink | Comments (17) | Post RSSRSS comment feed

Related posts

Comments

Pete us

Sunday, April 20, 2008 4:11 PM

Pete

Nicely done. That's the simplest explaination of the PHPBB3 login integration that I have heard so far.

Pete

Andy gb

Tuesday, April 22, 2008 10:09 AM

Andy

Thanks, Pete. I am to keep it clear.

Mike gb

Saturday, April 26, 2008 5:37 PM

Mike

Great article. I started off with this and made my own little amendments. I am primarily a .net developer, looking to add a bit of integration with phpbb3, but this should keep it universal. Apologies for any PHP mistakes.

I started with ucp.php and made a new page called custom_login.php.

This takes the form parameters as mentioned in your previous article, excluding the redirect one. They are then processed and XML is returned of a user element. If the login fails, the guest user is returned. All you need to do is check the is_registered node to check for an unsuccessful login.

I also took the login_box() function from functions.php and included this on custom_login.php.

I havent included any is_admin functionality, or login attempts. This could be added though by looking at the login_box() function.

Well, anyway, the code is as follows....

<?php
header("Content-type: text/xml");

define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
require($phpbb_root_path . 'common.' . $phpEx);
require($phpbb_root_path . 'includes/functions_user.' . $phpEx);
require($phpbb_root_path . 'includes/functions_module.' . $phpEx);

$mode = request_var('mode', '');

if ($mode == 'login' || $mode == 'logout')
{
define('IN_LOGIN', true);
}

$user->session_begin();
$auth->acl($user->data);
$user->setup('ucp');

switch ($mode)
{
case 'login':
if (!$user->data['is_registered'])
{
custom_login_box(request_var('redirect', "index.$phpEx"));
}


$xml_output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
$xml_output .= "<user>\n";

foreach ($user->data as $attrib => $val)
{
if(!$val )
{
$xml_output .= " <".$attrib.">NULL</".$attrib.">\n";
}
else
{
$xml_output .= " <".$attrib.">".$val."</".$attrib.">\n";
}
}

$xml_output .= "</user>";



echo $xml_output;

break;

case 'logout':
if ($user->data['user_id'] != ANONYMOUS && isset($_GET['sid']) && !is_array($_GET['sid']) && $_GET['sid'] === $user->session_id)
{
$user->session_kill();
$user->session_begin();
echo "logged out";
}
break;

case 'delete_cookies':

$set_time = time() - 31536000;

foreach ($_COOKIE as $cookie_name => $cookie_data)
{
$cookie_name = str_replace($config['cookie_name'] . '_', '', $cookie_name);

// Polls are stored as {cookie_name}_poll_{topic_id}, cookie_name_ got removed, therefore checking for poll_
if (strpos($cookie_name, 'poll_') !== 0)
{
$user->set_cookie($cookie_name, '', $set_time);
}
}

$user->set_cookie('track', '', $set_time);
$user->set_cookie('u', '', $set_time);
$user->set_cookie('k', '', $set_time);
$user->set_cookie('sid', '', $set_time);

$user->session_kill();
$user->session_begin();
break;
}

function custom_login_box()
{
global $db, $user, $template, $auth, $phpEx, $phpbb_root_path, $config;

if (empty($user->lang))
{
$user->setup();
}

if (isset($_POST['login']))
{
$password = request_var('password', '', true);
$username = request_var('username', '', true);

$auth->login($username, $password, $autologin, $viewonline, $admin);
}
}
?>

So all you need to do is point your login form at that. It logs into phpbb, and from the resultant xml, you can login on your site, and pull the session_id to perhaps append to the forum urls if need be.

Let me know if you think anything could be changed!

Cheers

Mike

Denise Skidmore us

Tuesday, April 29, 2008 12:37 PM

Denise Skidmore

Thanks! Very helpful article! I'd gotten halfway there on my own, but was getting stuck. Looking forward to trying your code tonight.

PCGT us

Friday, May 30, 2008 9:07 PM

PCGT

So I can add this into my HTML website code and it will work? Or do I need a certain type of format to work.

Andy gb

Wednesday, June 04, 2008 11:02 AM

Andy

The file in which you use this code MUST have a ".php" extension, else the code within will not be interpreted.

Psycho us

Sunday, June 08, 2008 7:10 AM

Psycho

Where would the class for user data be held? I'm wondering because I want to echo out the User information such as Name, Avatar, Rank, and a link to the profile settings page.

Andy gb

Tuesday, June 10, 2008 10:15 AM

Andy

It's all detailed in the article. It's all held in array called $user_data within the standard user class.

Using the code I gave the article to get a list of the available array entries (which represent the database columns)

Jack gb

Wednesday, July 02, 2008 10:59 AM

Jack

Hi there, the first article you wrote I was able to follow easily. However, I'm not very good with PHP, so I'm afraid I don't know what to do with this code. I do have a phpBB3 forum installed, so I'm able to successfully log in, but this code stumps me, so any help at all that you could provide me with would be greatly appreciated.

RadGH us

Wednesday, July 02, 2008 2:57 PM

RadGH

[phpBB Debug] PHP Notice: in file /includes/session.php on line 916: Cannot modify header information - headers already sent by (output started at /home/public_html/index.php:4)

I get this error three times with just the first block of code, after changing my directory. Everything works fine until I clear my cookies. Once I visit my forums again (even without logging in) it goes away. Does this mean a global is already declared or something?

Erwin nl

Wednesday, July 16, 2008 8:33 AM

Erwin

Woooooow! This is just what I needed. THANKS!

Matt au

Saturday, July 19, 2008 7:30 AM

Matt

[b]SPOT ON ![/]

I'm writing a membership renewal routine for my rowing club and I'd already coded and tested the base routines outside phpbb3.

It took me about 10 minutes to add this code at the top of my php and has allowed me to move on to the next step of accessing system variables for DB updates etc.
Now all I have to do is the ugly bit of actually making my routines 'plug-in' to phpbb3 - but that's another story ;)

Thanks very much.

thexteam in

Monday, August 18, 2008 8:03 PM

thexteam

For some reason it is unable to get the session from a script that is run one folder above this. I have the index.php in the root folder and the phpBB installation in a folder "forum" under this. I changed the path '/PATH/TO/FORUMS' to the correct folder and it doesn't work for me. I notice that the forum urls have a variable sid which gets passed to each other. When I pass the same in my index.php?sid=blah, I get the user information.

Any help is appreciated.

Tom

Tuesday, August 19, 2008 4:51 AM

Tom

I can't quite get this to work. Where do I put the "is_registered"? If I put it last my system hangs on the "includes" and if I put it first it will ofc say "User not logged in" at first, but it still says the same when I log in.

thexteam in

Tuesday, August 19, 2008 11:42 PM

thexteam

Tom,

I agree with your thought. I do not see that phpBB uses the php sessions at all. So unless you are doing everything inside phpBB's framework, we don't have it available outside. One way is to push session_start() in the index.php and have the $user object within the session. Don't know how much of a mess up it'll do to the security Smile

I don't remember the URL but do google for MediaWiki phpBB integration and another once called Thyme. See if they help you - and if you find them useful, appreciate if you could leave a word here.

Rob us

Tuesday, September 16, 2008 1:41 PM

Rob

Hello, I am getting several errors that I believe are attributed to security on my hosts side. If anyone knows of a workaround please let me know. I am fairly new to php so the simplest terms would be appreciated.

Errors:

Warning: include(common.php) [function.include]: failed to open stream: No such file or directory in /home/rxp/public_html/index.php on line 372

Warning: include(common.php) [function.include]: failed to open stream: No such file or directory in /home/rxp/public_html/index.php on line 372

Warning: include() [function.include]: Failed opening 'common.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/rxp/public_html/index.php on line 372

Warning: include(includes/functions_display.php) [function.include]: failed to open stream: No such file or directory in /home/rxp/public_html/index.php on line 373

Warning: include(includes/functions_display.php) [function.include]: failed to open stream: No such file or directory in /home/rxp/public_html/index.php on line 373

Warning: include() [function.include]: Failed opening 'includes/functions_display.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/rxp/public_html/index.php on line 373


code:

<?php define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : 'http://www.rxpgaming.com/forums/';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);
include($phpbb_root_path . 'includes/functions_display.' . $phpEx);
// Start session management
$user->session_begin();
$auth->acl($user->data);
foreach ($user->data as $sess => $val)
{
if(!$val )
{
print($sess ."=> NULL");
}
else
{
print($sess ."=>".$val);
}
print("<br />
");
}
?>

Derek us

Sunday, December 14, 2008 6:41 PM

Derek

I was glad to find this information, however I have one problem. What I want to do is have a link-forum on the index page that will take you to a php links page, where users can add links of their own. So, I want to keep it as a part of the phpBB forums, using the current logged in values. So from the index page of phpBB there is a link that goes to "links.php". I have done exactly as you have instructed so far in my links.php script, but when I view the user information array, it says that the user is not logged in and that the username is anonymous. When I go back to the phpBB page, i am still logged in though...do you have any idea what is happening here and how I can fix it?

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

Thursday, January 08, 2009 8:22 PM