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.