Forum Moderators: coopster

Message Too Old, No Replies

PHPBB question

         

supermanjnk

8:05 pm on Jul 5, 2004 (gmt 0)

10+ Year Member



I have an area on my site that I would like to password protect, with username/passwords. I would like to know if it's possible to somehow call the user accounts from the PHPBB that I already have set up on my site so people don't have to have two seperate logins. Any help would be great, i'm a bit of a php newbie so I can't do it myself.

coopster

8:32 pm on Jul 5, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, supermanjnk!

You certainly could use the same file. You're going to have to learn some scripting though if you intend to use a login system to secure pages. What do you mean you can't do it yourself? Plenty of folks have learned how right here on WebmasterWorld. A quick search on this site will turn up plenty of resources, including links to tutorials, code samples, etc. Why not give it a whirl?

supermanjnk

8:42 pm on Jul 5, 2004 (gmt 0)

10+ Year Member



I meant with my current knowledge I can't do it myself, not that I won't one day be able to but with the little time I have on my hand I don't get much time to learn scripting.

ItalianMob

8:55 pm on Jul 5, 2004 (gmt 0)

10+ Year Member



sure you can do it just look in your phpbb dB and find the table with the user data then add the following to the head section of your pages you want secure

***** BEGIN CODE ******\/

<?php

// File Name: login.php
// Check to see if $PHP_AUTH_USER already contains info
$host = "localhost";
$user = "ENTER_YOUR_dB_USER_NAME_HERE";
$pass = "AND_YOUR_PASSWORD_HERE";
$db = "DATABASE_NAME_HERE";
$utable = "TABLE_NAME_HERE";
mysql_connect($host,$user,$pass) or die("Unable to connect to database");
@mysql_select_db("$db") or die("Unable to select database $db");
if (!isset($PHP_AUTH_USER)) {

// If empty, send header causing dialog box to appear

header('WWW-Authenticate: Basic realm="SOME FRIENDLY NAME HERE"');
header('HTTP/1.0 401 Unauthorized');
exit;

} else if (isset($PHP_AUTH_USER)) {

// Formulate the query

$sql = "SELECT * FROM $utable WHERE usr_name='$PHP_AUTH_USER' and usr_pwd='$PHP_AUTH_PW'";

// Execute the query and put results in $result

$result = mysql_query($sql);

// Get number of rows in $result. 0 if invalid, 1 if valid.

$num = mysql_numrows($result);

if ($num!= "0") {
header("Location: secure.page.name.php");
exit;

} else {

header('WWW-Authenticate: Basic realm="ANY MESSAGE YOU WANT THE WINDOW TO SAY HERE"');
header('HTTP/1.0 401 Unauthorized');
echo 'Authorization Required.';
exit;

}

}

?>

***** END CODE ******^

Remember that has to be before any HTML coding on your PHP pages.

ItalianMob

8:59 pm on Jul 5, 2004 (gmt 0)

10+ Year Member



Oh ya I forgot you'll need to replace the column names in the sql query string

$sql = "SELECT * FROM $utable WHERE usr_name='$PHP_AUTH_USER' and usr_pwd='$PHP_AUTH_PW'";

usr_name will need to be the column name in the phpbb table for users where the user name is stored and the same thing for usr_pwd

henry0

10:12 pm on Jul 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



ItalianMob and Supermanjnk’
Welcome on Webmaster World

Italianmob This is nice and kind of you
However I remember the time I had almost no ideas on what I wanted to achieve
Not sure I still do :)
Many members such as Coopster helped me out by suggesting, letting me learn from my errors and pointing me to the correct route
You will seldom see full scripts posted (although I remember I have done it once - Mea Culpa! – )
In order for Supermanjnk to get something out of it
Would you be kind enough in adding a few in depth comments and reason why...
Your existing comments are very meaningful if you have already a basic knowledge
Please remember that Supermanjnk mentioned that he was pretty much inexperimented

By advance thank you
Hope I did not offend you
We may all learn from precise comments
Best regards

Henry

ItalianMob

12:05 am on Jul 6, 2004 (gmt 0)

10+ Year Member



naw not in the slightest. I've alway found it easier to have the code and disassemble it to see how it "ticks" if you will.

But each person has their own style of learning.

But if he wants some good books on PHP I would suggest the following two books.

PHP Programming by WROX
PHP and MySQL Web Development by SAMS

those are two excellent books if you a really serious about learning dynamic web content with PHP. Especially the later.

supermanjnk

3:46 am on Jul 6, 2004 (gmt 0)

10+ Year Member



Alright i've been playing with that script for awhile and i've gotten it working up to a point, right now it's sending back the errors.

Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in /home/youth22/public_html/auth.php on line 32

Warning: Cannot modify header information - headers already sent by (output started at /home/youth22/public_html/auth.php:32) in /home/youth22/public_html/auth.php on line 35

$sql = "SELECT * FROM $utable WHERE username='$PHP_AUTH_USER' and user_password='$PHP_AUTH_PW'";

// Execute the query and put results in $result

$result = mysql_query($sql);

// Get number of rows in $result. 0 if invalid, 1 if valid.

$num = mysql_numrows($result);

if ($num!= "0") {
header("Location: secure.page.name.php");
exit;

Is the code i have here. Something is wrong with this line:
$num = mysql_numrows($result);

I've tried changing it to mysql_num_rows but i still get the same result

ADDED:
I put in a die statement so it would tell me what the error is this is what i get back now:

1142: select command denied to user: 'youth22_Jasonk@localhost' for table 'phpbb_users'

That user has full access to the database, so I have no clue why the select command is being denied

coopster

8:03 am on Jul 6, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



That error states otherwise; you must have something incorrect in the security settings. Did you FLUSH PRIVILEGES after updating the permissions?

RonPK

11:40 am on Jul 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



supermanjnk, which version of phpBB are you using? If 2.0.x, things are a bit more complicated than just asking for http-authentication. That method will off course let users visit your secure pages, but if they decide to switch to the forum, they'll have to log in again. The same goes the other way around. So, if you're looking for a site-wide login, you may be better off using phpBB's login/session system.