1 (edited by n1mda 2007-04-02 21:14)

Topic: Combine PunBB login and registration with the main site

Guess I should say hi guys, I'm new to this forum and to PunBB also. So.. Hi guys! smile

Now to my question.

I'm working on a website that will have some functions that you need to be a member of the site to use, as file uploading and so on. I also want a forum for my members, so why not combine it all to one registration and one login. I'm not a total newbee in php and mysql, but I don't think it's a copy and paste solution for this.

I want a login form on my first page, and on the forum. Also the registration will be in another file so it doesn't link to www.mypage.com/forum/registration.php or something like that, I'm a simple guy and I want to keep it simple, like www.mypage.com/register or something.

Have someone of you made something like this before, or do you have any link to a guide or something for this?
There will be some problems with this, I know that for sure. But probably I will figure it out. Now I just want some hints and help to get started.

Best regards / n1mda

Re: Combine PunBB login and registration with the main site

If you don't have a login system already, then just use PunBB's as outlined in the integration instructions in PunBB's documentation.

Looking for a certain modification for your forum? Please take a look here before posting.

Re: Combine PunBB login and registration with the main site

This will add a login form to any page from which you call it.
If already logged in, it shows your username and group.

// Login Menu
    function login_menu() 
        {
        global $pun_user;
        if ($pun_user['is_guest'])
            {
            $stroutput= '<form id="login" method="post" action="'.PUN_ROOT.'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['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'].'" />
            <label for="req_username">Username: </label>
            <input type="text" id="req_username" name="req_username" size="4" maxlength="25" />
            <label for="req_password">Password: </label>
            <input type="password" id="req_password" name="req_password" size="4" maxlength="16" />
            <input type="submit" name="login" value="Login" />
            [<a href="'.PUN_ROOT.'register.php">Register</a>] 
            </form>';

            echo $stroutput;    
            }
        else 
            {
            $stroutput= '<p>Logged in as: '.pun_htmlspecialchars($pun_user['username']).' ('.$pun_user['g_user_title'].') [<a href="'.PUN_ROOT.'login.php?action=out&id='.$pun_user['id'].'">Logout</a>]</p>';
            echo $stroutput;    
            }
        }

I found it somewhere on this site (or Punres) so apologies for not crediting the original author.

--Alan

4 (edited by Tubby 2007-04-03 11:28)

Re: Combine PunBB login and registration with the main site

AlanCollier wrote:

This will add a login form to any page from which you call it.
If already logged in, it shows your username and group.

// Login Menu
    function login_menu() 
        {
        global $pun_user;
        if ($pun_user['is_guest'])
            {
            $stroutput= '<form id="login" method="post" action="'.PUN_ROOT.'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['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'].'" />
            <label for="req_username">Username: </label>
            <input type="text" id="req_username" name="req_username" size="4" maxlength="25" />
            <label for="req_password">Password: </label>
            <input type="password" id="req_password" name="req_password" size="4" maxlength="16" />
            <input type="submit" name="login" value="Login" />
            [<a href="'.PUN_ROOT.'register.php">Register</a>] 
            </form>';

            echo $stroutput;    
            }
        else 
            {
            $stroutput= '<p>Logged in as: '.pun_htmlspecialchars($pun_user['username']).' ('.$pun_user['g_user_title'].') [<a href="'.PUN_ROOT.'login.php?action=out&id='.$pun_user['id'].'">Logout</a>]</p>';
            echo $stroutput;    
            }
        }

I found it somewhere on this site (or Punres) so apologies for not crediting the original author.

--Alan

seems very nice smile, but is the 1st "echo $stroutput;" necessary? You are only declaring the variables with in the "if" statement. So simply add this jsut after the if statement:

echo $stroutput;

5

Re: Combine PunBB login and registration with the main site

Great!
Then I have som other things to fix also, like sessions and so on, but I will figure that out.

Thank you alot!

Re: Combine PunBB login and registration with the main site

tubby wrote:

..but is the 1st "echo $stroutput;" necessary?

Well spotted Tubby, sloppy coding on my behalf!

--Alan

Re: Combine PunBB login and registration with the main site

Moved to Integration

8

Re: Combine PunBB login and registration with the main site

I can't make it work!

This is my code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title></title>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
<link rel="stylesheet" type="text/css" href="styles/style.css" />
</head>
<body>
<div id="topleft"></div>
<div id="big">
        <div id="left_shadow"></div>
        <div id="right_shadow"></div>
<div id="superheader"></div>
<div id="supernavbar">
<ul>
<li><a href="../index.html" rel="self">Start</a></li>
<li><a href="../forum/index.php" rel="self">Forum</a></li>
<li><a href="../fotoalbum/index.php" rel="self">Photo</a></li>
<li><a href="../chat/index.php" rel="self">Chat</a></li>
<li><a href="../mix.html" rel="self">Mix</a></li>
<li2><a href="../login.html" rel="self" id="current">Login</a></li2>
</ul>
</div>
<div id="supercontainer">
<?php
define('PUN_ROOT', './forum/');
require PUN_ROOT.'include/common.php';
?>

<br>
<?php
// Login Menu
    function login_menu() 
        {
        global $pun_user;
        if ($pun_user['is_guest'])
            {
            $stroutput= '<form id="login" method="post" action="'.PUN_ROOT.'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['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'].'" />
            <label for="req_username">Username: </label>
            <input type="text" id="req_username" name="req_username" size="4" maxlength="25" />
            <label for="req_password">Password: </label>
            <input type="password" id="req_password" name="req_password" size="4" maxlength="16" />
            <input type="submit" name="login" value="Login" />
            [<a href="'.PUN_ROOT.'register.php">Register</a>] 
            </form>';

            echo $stroutput;    
            }
        else 
            {
            $stroutput= '<p>Logged in as: '.pun_htmlspecialchars($pun_user['username']).' ('.$pun_user['g_user_title'].') [<a href="'.PUN_ROOT.'login.php?action=out&id='.$pun_user['id'].'">Logout</a>]</p>';
            echo $stroutput;    
            }
        }
        ?>
       
</div>
<div id="superfooter">Copyright</div>
</div>

</body>
</html>

Please help me! hmm And what was it you should delete in the code?

Re: Combine PunBB login and registration with the main site

What error do you get? What doesn't work about it?

[No need to delete anything, it will still work, it's just inefficient code]

--Alan

10

Re: Combine PunBB login and registration with the main site

It doesn't show anything....
The page is completely blank, (the header and the navbar is still there and everything but no login) and if i look at the source code it nothing there! All PHP code are gone!

11 (edited by AlanCollier 2007-05-02 15:30)

Re: Combine PunBB login and registration with the main site

I see!

That's because the code above defines a function to spit out the necessary HTML.
You then need to call that function where you want the login stuff to be.

So simply put:
<?php login_menu(); ?>
Where you want the login box to appear.

The code is put into a function so that you can have it in a separate file so that you don't have to put the same code into all of your pages.

--Alan

12

Re: Combine PunBB login and registration with the main site

Thanks! big_smile Works perfectly!
You made my day!

Re: Combine PunBB login and registration with the main site

Cool.

Let us know when you've got the site up and running.
I'd be interested to see how you used it.

--Alan

14 (edited by Peter 2007-05-12 19:50)

Re: Combine PunBB login and registration with the main site

This looks like what I need, but there are some frustrating "blank spots" in this solution for someone who comes to this from html/css and doesn't really know PHP.

Has the "echo $stroutput;" issue been fixed? It's not obvious to me how to fix it in the code myself.

How do I make my sure my index.php can find the login_menu function? Simply putting <?php login_menu(); ?> where I want the login box doesn't do the trick. I need some other secret ingredient.

I've tried require('common/functions/login.php'); at the top of the page, but that produced a mess.

I know this is a dumb question, but if you're not thinking in PHP logic this is not obvious.

Edit:

OK, I figured it out. I added the login_menu function to functions.php in the include folder. And this to the index.php file:

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

Edit 2:
One little issue. When I log out it redirects to forum/index.php instead of main site homepage index.php. I have some ideas how to fix it, but what would be the cleanest way?

15

Re: Combine PunBB login and registration with the main site

I can't find a solution to that last little issue: when logging out it redirects to forum/index.php instead of main site homepage index.php. How can I fix that?

I tried replacing $destination_url =  in functions.php with the main site's URL, but it had no effect (and I probably messed up something else by doing that).

Can anyone help?

16

Re: Combine PunBB login and registration with the main site

I made this edit to redirect to the main homepage after logout instead of to forum/index.php:

else if ($action == 'out')
{
    if ($pun_user['is_guest'] || !isset($_GET['id']) || $_GET['id'] != $pun_user['id'])
    {
        header('Location: http://mywebsite.com');
        exit;
    }

    // Remove user from "users online" list.
    $db->query('DELETE FROM '.$db->prefix.'online WHERE user_id='.$pun_user['id']) or error('Unable to delete from online list', __FILE__, __LINE__, $db->error());

    // Update last_visit (make sure there's something to update it with)
    if (isset($pun_user['logged']))
        $db->query('UPDATE members SET last_visit='.$pun_user['logged'].' WHERE id='.$pun_user['id']) or error('Unable to update user visit data', __FILE__, __LINE__, $db->error());

    pun_setcookie(1, random_pass(8), time() + 31536000);

    redirect('http://mywebsite.com', $lang_login['Logout redirect']);
}

It seems to work, but could it cause problems elsewhere?

Re: Combine PunBB login and registration with the main site

Does the Logout button work for you guys ?

it does redirect to the forum but it doesnt let me logout

Any ideas ?

Re: Combine PunBB login and registration with the main site

Yea same here MalibuKiller; I like the login bit though pretty simple and cool =]
Just need the logout to work =p