1 (edited by zentropy 2008-01-10 20:48)

Topic: Login integration: Logging in on an existing site and punBB

After implementing registration integration (See: http://punbb.org/forums/viewtopic.php?id=17964 ), i'd like to also implement login integration.
I've seen many threads and tutorials about this but most were about login integration by starting out with punBB and building a site around that.
We have an existing site with accounts which are now also injected into the punbb_users table upon registration, but when someone logs in on our site that user will only get logged onto the website and not yet punbb.
Once a user is logged in we can fetch the username and password using php.
I'd like to know how to use those variables to also log into the forums when a user goes into the forums and if this would be the way to do it in our situation or if there is another (better?) way. Does anyone know any tutorials/documentation/wiki/thread regarding doing it this particular way or can someone perhaps explain how to accomplish this?
So in short we're starting out with an existing website with an existing log in system and we want to integrate punBB into the process instead of starting out with punBB and build around it.

Re: Login integration: Logging in on an existing site and punBB

Take a look at pun_setcookie and how login.php uses it.

3 (edited by zentropy 2008-01-10 22:45)

Re: Login integration: Logging in on an existing site and punBB

So am i understanding this correctly?

pun_setcookie($user_id, $form_password_hash, $expire);

If the variables(username & password) of the user which is logged in on the website are fed to the pun_setcookie() functions as parameters, then when the user visits the forum he/she will be logged in?
Although i'm wondering $user_id might not be the same as username, neither might $form_password_hash imply the hashed password of the user.
So does pun_setcookie() basically set a few variables of a user's punbb_cookie? (Variables username and password?)

Re: Login integration: Logging in on an existing site and punBB

Take a look at include/functions.php and you'll see exactly what it does. It does a call to setcookie using the various cookie settings in config.php along with the supplied data. And $user_id is the actual numeric user ID, not username.

5 (edited by zentropy 2008-01-10 23:32)

Re: Login integration: Logging in on an existing site and punBB

Where from does user_id come? I checked the users table, but i can't seem to find a column called user_id, only id and group_id.

Re: Login integration: Logging in on an existing site and punBB

It's the primary key, id

7 (edited by zentropy 2008-01-11 01:09)

Re: Login integration: Logging in on an existing site and punBB

Would the following code which i put in the login page of the website get the user logged in into the forum? (can't try it out right now)
Would this be a method that works?

 
    include "./punbb/include/functions.php"; //Make sure the following code has access to the pun_hash() and pun_setcookie() function definitions
    
    $username = $user->Username;  //Get the username of the logged in user and put it in a variable    
    
    $queryFromPunbb = "SELECT * FROM punbb_users WHERE username='$username'";    //get the id from the logged in user. This id is the primary key of the punbb_users table in the punbb database
    $result = mysql_query($queryFromPunbb, $conn) or die (mysql_error());    //Run the query
    $row = mysql_fetch_array($result) or die(mysql_error());    //Put the result of the query in an array called $row  
    
    $user_id = $row['id'];    //Put the id from the $row array into the variable $user_id
    $hashedPassword = pun_hash($user->Password);    //hash the user's password using the punbb hash function 
    
    pun_setcookie($user_id, $hashedPassword, time()+24*60*60);  //Set the punBB cookie, which is called punbb_cookie

Re: Login integration: Logging in on an existing site and punBB

Ok, i tried the above, setting the cookie, but it didn't get me logged in when visiting the forums.

Re: Login integration: Logging in on an existing site and punBB

Can you compare the cookie that was set to the cookie that is set when logging into PunBB properly? Maybe that will shed some light on the problem.

10 (edited by zentropy 2008-01-12 00:09)

Re: Login integration: Logging in on an existing site and punBB

I'm wondering though if running the pun_setcookie() function will be enough to log a user in into the forum. (Is that really all that is required to log in a user?)
The pun_setcookie function(as seen in the code i posted) is run from a file on our website, which is in the root directory, while punbb is in /punbb .

Re: Login integration: Logging in on an existing site and punBB

It should be, all that's checked to see if a user is logged in is a cookie.
And unless you modified the cookie path in config.php, I don't think there's an issue.

12 (edited by zentropy 2008-01-12 03:17)

Re: Login integration: Logging in on an existing site and punBB

This the the output i get from printing the content of the cookie using print_r($_COOKIE); exit; after running the script posted above.
With this output i don't get logged in. (Not sure if the content of the cookie is at fault offcourse)

Array
(
    [punbb_cookie] => a:2:{i:0;i:1;i:1;s:32:"684c51e53a07a80952d831dad741cc59";}
    [__utma] => 37521701.265511199.1199912689.1200091902.1200106214.15
    [__utmz] => 37521701.1199912689.1.1.utmccn=(direct)|utmcsr=(direct)|utmcmd=(none)
    [style_cookie] => printonly
    [__utmc] => 93837953
    [PHPSESSID] => 1c3bd95e6dhrs18pkr7aki8t40
    [phpbb3_2rojj_u] => 1
    [phpbb3_2rojj_k] => 
    [phpbb3_2rojj_sid] => 4e3437741406faee467a71d53f29fb98
    [a:2:{i:0;s:1:"3";i:1;s:32:"09202be3249d1bd81d509b9c9977da5b";}] => 
    [__utmb] => 37521701
)

Re: Login integration: Logging in on an existing site and punBB

$_COOKIE isn't updated until the next pageview wink

14 (edited by zentropy 2008-01-12 03:23)

Re: Login integration: Logging in on an existing site and punBB

Smartys wrote:

$_COOKIE isn't updated until the next pageview wink

This is the output from the cookie when using print_r($_COOKIE); on the punbb index.php page. (Am not logged in when this output displays)

Array
(
    [punbb_cookie] => a:2:{i:0;i:1;i:1;s:32:"684c51e53a07a80952d831dad741cc59";}
    [__utma] => 37521701.265511199.1199912689.1200091902.1200106214.15
    [__utmz] => 37521701.1199912689.1.1.utmccn=(direct)|utmcsr=(direct)|utmcmd=(none)
    [style_cookie] => printonly
    [__utmc] => 93837953
    [PHPSESSID] => 1c3bd95e6dhrs18pkr7aki8t40
    [phpbb3_2rojj_u] => 1
    [phpbb3_2rojj_k] => 
    [phpbb3_2rojj_sid] => 4e3437741406faee467a71d53f29fb98
    [a:2:{i:0;s:1:\"3\";i:1;s:32:\"09202be3249d1bd81d509b9c9977da5b\";}] => 
    [__utmb] => 37521701
)

Re: Login integration: Logging in on an existing site and punBB

And this is the output on index.php when i'm successfully logged in:

Array
(
    [punbb_cookie] => a:2:{i:0;s:1:"3";i:1;s:32:"5ec70ea77e7b20ef464b8489d391a07b";}
    [__utma] => 37521701.265511199.1199912689.1200091902.1200106214.15
    [__utmz] => 37521701.1199912689.1.1.utmccn=(direct)|utmcsr=(direct)|utmcmd=(none)
    [style_cookie] => printonly
    [__utmc] => 93837953
    [PHPSESSID] => 1c3bd95e6dhrs18pkr7aki8t40
    [phpbb3_2rojj_u] => 1
    [phpbb3_2rojj_k] => 
    [phpbb3_2rojj_sid] => 4e3437741406faee467a71d53f29fb98
    [a:2:{i:0;s:1:\"3\";i:1;s:32:\"09202be3249d1bd81d509b9c9977da5b\";}] => 
    [__utmb] => 37521701
)

Re: Login integration: Logging in on an existing site and punBB

The cookie set, in other words, is not the same.

17 (edited by zentropy 2008-01-12 03:36)

Re: Login integration: Logging in on an existing site and punBB

Smartys wrote:

The cookie set, in other words, is not the same.

What does that imply in this instance? Could it be that the password somehow does not hash correctly?

Re: Login integration: Logging in on an existing site and punBB

You can't just include functions.php: well, you can, but since you're lacking common.php you're missing out on lots of variables. I think you should try including config.php.

19 (edited by zentropy 2008-01-12 03:53)

Re: Login integration: Logging in on an existing site and punBB

This is what i get when echo-ing out the variables right before i pass them as parameters to the pun_setcookie() function.
In sequential order: $user_id $hashedPassword time()+24*60*60

3 206c80413b9a96c1312cc346b7d2517b84463edd 1200195464

Re: Login integration: Logging in on an existing site and punBB

Smartys wrote:

You can't just include functions.php: well, you can, but since you're lacking common.php you're missing out on lots of variables. I think you should try including config.php.

When including config.php it still seems to not log me in. I also tried including common.php, though when doing that i get the following error upon going to the forums:

Fatal error: Cannot redeclare check_cookie() (previously declared in /root/punbb/include/functions.php:28) in /root/punbb/include/functions.php on line 112

Re: Login integration: Logging in on an existing site and punBB

Which means don't include functions.php when including common.php since common.php takes care of that for you wink

22 (edited by zentropy 2008-01-12 04:30)

Re: Login integration: Logging in on an existing site and punBB

Smartys wrote:

Which means don't include functions.php when including common.php since common.php takes care of that for you wink

Victory! That did the trick. It's working now.
Thanks Smartys, i really appreciate it you took the time to help me out. Thanks and Kudos!

23 (edited by zentropy 2008-01-12 04:56)

Re: Login integration: Logging in on an existing site and punBB

Furthermore for people who will stumble upon this thread and try to accomplish the same, here is the full working code:

<? 
define('PUN_ROOT', './punbb/');
include 'punbb/include/common.php';    //Include this file so that $pun_user['is_guest'] can be used and that (some?) functions from punbb/include/functions.php can also be used in this script


if (($websitelogin==true) && ($pun_user['is_guest'])) {
    
    
    include "./punbb/customforumdbconfig.php"; //Custom file which contains and sets the variables for setting up a connection to the punbb database
    include "opendb.php"; //Script that uses the above mentioned variables to open the actual connection with the database
    
    
    $username = $user->Username;  //Get the username of the logged in user and put it in a variable     ($user->Username comes from code from the website which is intended to get login integration with punbb through this script)
    
    $queryFromPunbb = "SELECT * FROM punbb_users WHERE username='$username'";    //get the row with the id from the logged in user. This id is the primary key of the punbb_users table in the punbb database
    $result = mysql_query($queryFromPunbb, $conn) or die (mysql_error());    //Run the query
    $row = mysql_fetch_array($result) or die(mysql_error());    //Put the result of the query in an array called $row  
    
    $user_id = $row['id'];    //Put the id from the $row array into the variable $user_id
    $hashedPassword = pun_hash($user->Password);    //hash the user's password using the punbb hash function 
    $expirationTime = time()+24*60*60;
    
    pun_setcookie($user_id, $hashedPassword, $expirationTime);  //Set the punBB cookie, which is called punbb_cookie
    
    include "closedb.php"; //Include a script which closes the connection with the punbb database

header("Location: ./punbb"); //send the browser to the /punbb directory
}
elseif (($websitelogin==true) && !($pun_user['is_guest'])) {
    header("Location: ./punbb"); //send the browser to the /punbb directory
}
elseif (!($websitelogin) && ($pun_user['is_guest'])) {
    header("Location: ./punbb"); //send the browser to the /punbb directory
}
else {
    exit("Error: website login flag and/or guest flag exception"); //Print error to screen and terminate the execution of the script
} 
?>

Re: Login integration: Logging in on an existing site and punBB

Just FYI, if you're using common.php you can use PunBB's dblayer: the connection already exists.

25

Re: Login integration: Logging in on an existing site and punBB

I have written a function for integration of PunBB login in another website. Idea is to use login infos to login into punBB
It used to work but not anymore. Here is the function :


define('PUN_ROOT', 'modules/forum/');
require PUN_ROOT.'include/common.php';

$mysql = mysql_connect("HOST", "USER", "PASS");
mysql_select_db( "MYWEB" );

$result_pun = mysql_query("select `id` from `forum_users` where `username`='$userx' ");
$row_pun = mysql_fetch_array($result_pun);
pun_setcookie ($row_pun['id'], pun_hash("$passwordx"), time() + 86400);

I get the error :

///login of a user into punBB forum
Warning: Cannot modify header information - headers already sent by (output started at /home/sparta/www/htdocs/marsweb/ForumLogin.php:3) in /home/sparta/www/htdocs/marsweb/modules/forum/include/functions.php on line 174

And I cannot login...
Any ideas or suggestions ?