Login Integration Tutorial

This forum is now closed as part of retiring phpBB2.
Forum rules
READ: phpBB.com Board-Wide Rules and Regulations

This forum is now closed due to phpBB2.0 being retired.
Josh18657
Registered User
Posts: 425
Joined: Wed Nov 30, 2005 9:55 pm
Contact:

Login Integration Tutorial

Post by Josh18657 »

I found the Login Integration Tutorial (Sessions) in the knowledgebase
http://www.phpbb.com/kb/article.php?article_id=143
but for some reason I can not get it to work correctly. If I open the phpBB log in page and log in the sessions code at the top of individule pages works fine. The problem is that the log in script does not seam to work. I do not get any error messages either. I if not change the extensions.inc or common.php files at all.

Here is my log in form, named logintestform.htm :

Code: Select all

<html>
<head>
<title></title>
</head>


<body bgcolor="#FFFFFF">

<form action="logintest.php" method="post">
Username<input type="text" name="username"><br />
Password<input type="password" name="password"><br /> 
<input type="submit" value="login" name="login"> 
</form>


</body>


</html>
And here is what I have at the top of the page called logintest.php :

Code: Select all

<?php

define('IN_PHPBB', true); 
$phpbb_root_path = 'phpBB2/';
  include($phpbb_root_path . 'extension.inc');
include($phpbb_root_path . 'common.'.$phpEx); 

// 
// Start session management 
// 
$userdata = session_pagestart($user_ip, PAGE_INDEX); 
init_userprefs($userdata); 
// 
// End session management 
//
if( $userdata ['session_logged_in'] ) 
   { 
      echo('Welcome');
   } 
else 
       { 
               echo('Please Login'); 
       }

?>


<html>
<head>
<title></title>
</head>


<body bgcolor="#FFFFFF">

</body>


</html>
Any help with this would be greatly apreciated.

Josh
Josh18657
Registered User
Posts: 425
Joined: Wed Nov 30, 2005 9:55 pm
Contact:

Post by Josh18657 »

Sorry for posting in the wrong section originally.

I don't think it can be anything to dificult that I have goofed up, I just don't see where most of the info comes from.
agent00shoe
Registered User
Posts: 373
Joined: Sat Feb 12, 2005 9:39 pm

Post by agent00shoe »

Try posting to login.php instead by changing

Code: Select all

action="logintest.php"
to

Code: Select all

action="login.php"
If you want to be redirected to a certain page after logging in, you can use a hidden field in the login form like this

Code: Select all

<input type="hidden" name="redirect" value="viewforum.php?f=111&start=0" />
This hidden field would redirect to viewforum.php?f=11.
Josh18657
Registered User
Posts: 425
Joined: Wed Nov 30, 2005 9:55 pm
Contact:

Post by Josh18657 »

Thank you very much, that worked great.

Now I have another question, I want to make a log out button that shows up instead of the form if the user is logged in. I have everything set up so it shows the button but when the button is clicked it just goes to a page that shows an invalid session. I guess somehow I have to pass along the session id in the form as well, any idea how I could do this?

Code: Select all

<?php

define('IN_PHPBB', true); 
$phpbb_root_path = '';
  include($phpbb_root_path . 'extension.inc');
include($phpbb_root_path . 'common.'.$phpEx); 

// 
// Start session management 
// 
$userdata = session_pagestart($user_ip, PAGE_INDEX); 
init_userprefs($userdata); 
// 
// End session management 
//
if( $userdata ['session_logged_in'] ) 
   { 
      echo('
<form action="login.php" method="post">
<input type="hidden" name="logout" value="true" />
<input type="hidden" name="redirect" value="logintest.php?f=111&start=0" />
<input type="submit" value="logout" name="logout">
</form>');
   } 
else 
       { 
               echo('

<form action="login.php" method="post">
Username<input type="text" name="username"><br />
Password<input type="password" name="password"><br />
<input type="hidden" name="redirect" value="logintest.php?f=111&start=0" />
<input type="submit" value="login" name="login"> 
</form>

'); 
       }

?>
agent00shoe
Registered User
Posts: 373
Joined: Sat Feb 12, 2005 9:39 pm

Post by agent00shoe »

Josh18657 wrote: Thank you very much, that worked great.

Now I have another question, I want to make a log out button that shows up instead of the form if the user is logged in. I have everything set up so it shows the button but when the button is clicked it just goes to a page that shows an invalid session. I guess somehow I have to pass along the session id in the form as well, any idea how I could do this?

Code: Select all

<?php

define('IN_PHPBB', true); 
$phpbb_root_path = '';
  include($phpbb_root_path . 'extension.inc');
include($phpbb_root_path . 'common.'.$phpEx); 

// 
// Start session management 
// 
$userdata = session_pagestart($user_ip, PAGE_INDEX); 
init_userprefs($userdata); 
// 
// End session management 
//
if( $userdata ['session_logged_in'] ) 
   { 
      echo('
<form action="login.php" method="post">
<input type="hidden" name="logout" value="true" />
<input type="hidden" name="redirect" value="logintest.php?f=111&start=0" />
<input type="submit" value="logout" name="logout">
</form>');
   } 
else 
       { 
               echo('

<form action="login.php" method="post">
Username<input type="text" name="username"><br />
Password<input type="password" name="password"><br />
<input type="hidden" name="redirect" value="logintest.php?f=111&start=0" />
<input type="submit" value="login" name="login"> 
</form>

'); 
       }

?>


Try replacing

Code: Select all

<form action="login.php" method="post">
<input type="hidden" name="logout" value="true" />
<input type="hidden" name="redirect" value="logintest.php?f=111&start=0" />
<input type="submit" value="logout" name="logout">
</form>
with

Code: Select all

<form action="login.php" method="get">
<input type="hidden" name="logout" value="true" />
<input type="hidden" name="redirect" value="logintest.php?f=111&start=0" />
<input type="submit" value="logout" name="logout">
</form>
I just changed the method to GET since the normal logout link uses GET vars. I didn't test this and don't know if it'll work or if it redirects on logging out, but you can give it a shot.
Josh18657
Registered User
Posts: 425
Joined: Wed Nov 30, 2005 9:55 pm
Contact:

Post by Josh18657 »

That didn't work either :(

From the looks of the phpBB log out button I need to be able to send along the SID (Sessions ID?) but I don't know how I can do this.

Josh
agent00shoe
Registered User
Posts: 373
Joined: Sat Feb 12, 2005 9:39 pm

Post by agent00shoe »

I forgot about the new session handling for 2.0.18 and I'm not sure how it would be used here. :?
Josh18657
Registered User
Posts: 425
Joined: Wed Nov 30, 2005 9:55 pm
Contact:

Post by Josh18657 »

So you think if I went back 1 version this would work fine? And if so where can I find an older version? I only see the newest one on the downloads page.

Thanks
Josh
agent00shoe
Registered User
Posts: 373
Joined: Sat Feb 12, 2005 9:39 pm

Post by agent00shoe »

I don't know offhand where to find older versions, but, yes, it would(should) work. I still use 0.15 for my pages since I just use the login part and not the forum. :D
Josh18657
Registered User
Posts: 425
Joined: Wed Nov 30, 2005 9:55 pm
Contact:

Post by Josh18657 »

Ok after reading the other post in this area I think I am close but I am doing something wrong writting the code. can you tell me how I would write this correctly? Does php work inside php? I'll show ya what I mean.

Code: Select all

if( $userdata ['session_logged_in'] ) 
   { 
      echo('
<form action="login.php" method="GET">
<input type="hidden" name="logout" value="true" />
<input type="hidden" name="sid" value="echo $userdata['session_id'];" />
<input type="hidden" name="redirect" value="logintest.php?f=111&start=0" />
<input type="submit" value="log out" name="log out">

');
Cause if put echo $userdata['session_id'] by itself it does show the SID but I don't know how to correctly put it in the script above.

Josh
Blankety Blank Man
Registered User
Posts: 881
Joined: Wed Mar 30, 2005 3:54 am

Post by Blankety Blank Man »

echo prints to the html output. It does not have to be a function (with parentheses), and you need to close the string at value=" and open it again at " />, using period (.) to concatenate the variable into the string.
try using this (no echo inside of echo) insead:

Code: Select all

if( $userdata ['session_logged_in'] )
   {
      echo '
<form action="login.php" method="GET">
<input type="hidden" name="logout" value="true" />
<input type="hidden" name="sid" value="' . $userdata['session_id']; . '" />
<input type="hidden" name="redirect" value="logintest.php?f=111&start=0" />
<input type="submit" value="log out" name="log out">

';
Josh18657
Registered User
Posts: 425
Joined: Wed Nov 30, 2005 9:55 pm
Contact:

Post by Josh18657 »

Parse error: parse error, unexpected '.' in /home/wolves/public_html/phpBB2/logintestform.php on line 21


Error I get with that. :(

line 21 is this:

Code: Select all

<input type="hidden" name="sid" value="' . $userdata['session_id']; . '" />
User avatar
MHobbit
Former Team Member
Posts: 4761
Joined: Thu Mar 18, 2004 5:32 pm
Location: There and Back Again

Post by MHobbit »

Remove the semicolon from after the session_id variable.
Former phpBB MOD Team member
No private support is offered.
"There’s too many things to get done, and I’m running out of days..."
Josh18657
Registered User
Posts: 425
Joined: Wed Nov 30, 2005 9:55 pm
Contact:

Post by Josh18657 »

THANKS GUYS :D

Ok here is the completed script. I guess for now it should be used inside an iframe. If the user is not logged in they will see the log in form, if the user is logged in they will see the log out button. I think I will add a message if the user is logged in saying Welcome (username).

Code: Select all

<?php

define('IN_PHPBB', true); 
$phpbb_root_path = '';
  include($phpbb_root_path . 'extension.inc');
include($phpbb_root_path . 'common.'.$phpEx); 

// 
// Start session management 
// 
$userdata = session_pagestart($user_ip, PAGE_INDEX); 
init_userprefs($userdata); 
// 
// End session management 
//
if( $userdata ['session_logged_in'] ) 
   { 
      echo ' 
<form action="login.php" method="GET"> 
<input type="hidden" name="logout" value="true" /> 
<input type="hidden" name="sid" value="' . $userdata['session_id'] . '" /> 
<input type="hidden" name="redirect" value="logintestform.php?f=111&start=0" /> 
<input type="submit" value="log out" name="log out">
';
   } 
else 
       { 
               echo('

<form action="login.php" method="post">
Username<input type="text" name="username"><br />
Password<input type="password" name="password"><br />
<input type="hidden" name="redirect" value="logintestform.php?f=111&start=0" />
<input type="submit" value="login" name="login"> 
</form>

'); 
       }
 

?>
Josh18657
Registered User
Posts: 425
Joined: Wed Nov 30, 2005 9:55 pm
Contact:

Post by Josh18657 »

Since you all are such a great help I was wondering if you could help me with something else.
I want to add some code on a page that allows me to restrict it to just logged in members and redirect others back to the index page.
I think my code is totally wrong and I also don't know how to change
$userdata ['session_logged_in']
so it looks for users not logged in
here is part of a page that I have so far, I do have the <?php and ?> tags on the page

Code: Select all

$members_only = "1"; //anyone = 1, members only = 2
$page_name = ($userdata ['username']) ;  //must edit to match name displayed on page
$page_title = "$page_name"; //will be displayed in title after $settings['website_title']
                            //with this setting page title is the same as page name
//
//
//
//


if( $members_only == 2 and $userdata ['session_logged_in'] ) 
   { 
    header("location: index.php"); exit;
   }
Post Reply

Return to “[2.0.x] MOD Writers Discussion”