Topic: Steps to Integrate Login/Logout
I've seen a bunch of different posts on this topic and none of them was really doing the job for me -- probably b/c I'm fairly new to PHP and didn't quite get what other people were doing. If you're looking for simple steps to simply use PunBB's user login system across your site (ie: having a box on the homepage where people can sign in, or that conversely recognizes that they're already signed in and offers them the option to sign out) here's a pretty simple way to do it.
1. For practice purposes, create a new blank php file called "pun_test.php".
2. Put the following two lines at the top:
define('PUN_ROOT', './forums');
include 'forums/include/common.php';
In the first line, ./forums should be the relative path to your PunBB files from the pun_test.php file. In the second line, forums should be replaced with whatever you have called that folder.
3. I chose to put the login/logout stuff in a function. That way I can add it to any page on my site just by calling the function. For example purposes, make another php file and call it login_function.inc.php. The ".inc" isn't necessary, but as your site gets more complex it will help you keep different types of files organized.
4. Your pun_test.php file needs to know the location of your function file, so add the following line:
include './login_function.inc.php'
Again, that path is assuming it's in the same folder as pun_test.php. If not, you'll need to change your path accordingly.
5. Last thing you'll need in your pun_test.php file is the actual call to the function. I called the function login_menu, so all you need to add is:
login_menu();
Your finished pun_test.php file should look like this:
<?php
define('PUN_ROOT', './forums');
include 'forums/include/common.php';
include './login_function.inc.php'
login_menu();
?>
6. Now for the function. The first thing you need to do is name the function you're going to define.
<?php
function login_menu() {
7. Next you need to declare some global variables that are used by PunBB. What does this mean? Simply that PunBB assigns values to a bunch of variables -- like user name, password, etc. -- and makes them available for use throughout the site. But your function won't know they are available until you specifically tell it so. All you need for the login/logout box are the values stored in $pun_user:
global $pun_user;
8. First thing we're going to do in the function is see if the user is logged in. If they aren't, we want them to see the login box. This part I took directly from someone else's post (my apologies, I don't remember which one. Thanks whoever you are!)
if ($pun_user['is_guest']) {
echo "<form id=\"login\" method=\"post\" action=\"/forums/login.php?action=in\" onsubmit=\"return process_form(this)\">
<input type=\"hidden\" name=\"form_sent\" value=\"1\" />
<input type=\"hidden\" name=\"redirect_url\" value=\"".$_SERVER['SCRIPT_NAME']."\" />
<input type=\"text\" name=\"req_username\" size=\"25\" maxlength=\"25\" />
<input type=\"password\" name=\"req_password\" size=\"16\" maxlength=\"16\" />
<input type=\"submit\" name=\"login\" value=\"Login\" />
</form>";
}
Again, this is looking at the 'is_guest' value of $pun_user. If it is set, meaning they are a guest, then it shows the following lines of code which make the form. You can edit this to use your own styles, fonts, etc. This is just a super basic thing with two fields for user and password and a submit button. The person who originally posted made a nice addition to his code though with the line that reads "<input type=\"hidden\" name=\"redirect_url\" ... " etc. That line just says that when someone submits the form, come back to whatever page the form is on. So if you add the box to your homepage, when someone logs in they'll be taken back to your homepage, not to the PunBB index page.
9. The other option is that someone is already logged in. In that case you want to show them a nice little "Welcome [username]" type message and a link to log back out. Here's the code for that part:
else {
echo "Logged in as: ".pun_htmlspecialchars($pun_user['username'])."<br> Last visit: ". format_time($pun_user['last_visit'])."
<br>Click <a href=\"/forums/login.php?action=out&id=".$pun_user['id']."&location_out=".$_SERVER['SCRIPT_NAME']."\">here</a> to log out.</div>";
}
10. In my logout link I've passed a new variable, "$location_out". That's because I want someone logging out to return to the same page, not to the PunBB login screen. A couple of quick modifications to the login.php file in PunBB make this work.
11. Last thing you need is your final } to close out the function and the ?> to close out the page. Your complete login_function.inc.php file should look like this:
<?php
function login_menu() {
global $pun_user;
if ($pun_user['is_guest']) {
echo "<form id=\"login\" method=\"post\" action=\"/forums/login.php?action=in\" onsubmit=\"return process_form(this)\">
<input type=\"hidden\" name=\"form_sent\" value=\"1\" />
<input type=\"hidden\" name=\"redirect_url\" value=\"".$_SERVER['SCRIPT_NAME']."\" />
<input type=\"text\" name=\"req_username\" size=\"25\" maxlength=\"25\" />
<input type=\"password\" name=\"req_password\" size=\"16\" maxlength=\"16\" />
<input type=\"submit\" name=\"login\" value=\"Login\" />
</form>";
}
else {
echo "Logged in as: ".pun_htmlspecialchars($pun_user['username'])."<br> Last visit: ". format_time($pun_user['last_visit'])."
<br>Click <a href=\"/forums/login.php?action=out&id=".$pun_user['id']."&location_out=".$_SERVER['SCRIPT_NAME']."\">here</a> to log out.</a>";
}
}
?>
10. Okay, you're just about done here. Last step is to open up the login.php file in your PunBB folder. Go to line 85 which reads: else if ($action == 'out'). This is the piece of code that handles processing of a log out request for PunBB.
Right after line 86, (which is simply a curly bracket { ), I added the following lines:
if(isset($_GET['location_out'])){
$location_out=$_GET['location_out'];
}
else{
$location_out="index.php";
}
What those do is look to see if you're logging out from a non-PunBB page on your site. If you are, our logout function will be passing the location_out variable and PunBB will use this as the address to redirect you to after logging out. If the user was in your boards, however, there won't be a location_out variable b/c they'll be using PunBB's built in logout script. So in that case we simply set location_out to the same value PunBB was using originally.
11. Finally, go to what is now line 108. It should read: redirect('index.php', $lang_login['Logout redirect']);
Change that to: redirect($location_out, $lang_login['Logout redirect']);
That's it. If you open your pun_test.php file in a browser now you'll see your basic login form. Put in your user name and pass and submit and it will take you back to that same page, only now it will show you your user name, the last time you logged in, and a link to log out. If you manually go to your PunBB index page you'll see that it recognizes that you're logged in. Anywhere on your site you want to include your new login box just put the three lines at the start of the file (the include / define / include that are at the start of pun_test.php) and then wherever on your page you want the box to appear just call your new function: login_menu();
As you play around you'll see that it is easy to change what info. it shows once you are logged in, the look of the form, etc.
Scanned over for typos but let me know if you have any problems with this or need any help.