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 2008


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 7 people

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

Posted by Andy on Wednesday, April 02, 2008 2:51 PM
Permalink | Comments (10) | 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?

Add comment


(Will show your Gravatar icon)  

  Country flag

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



Live preview

Saturday, July 05, 2008 3:25 AM