Topic: Log on form the website

I would like to make a log on form on my website, how to do it?

Re: Log on form the website

*lol*
I had this same issue, and it was so easy.
I just  looked at the source of the login screen and copy/pasted. Then styled to fit my page.

Simple and effective! wink

3 (edited by macius 2005-02-28 07:34)

Re: Log on form the website

I can't find it in login.php can you post the correct form code?
And how can I check if the user is logged in and what are his rights (is he a moderator or not)?

Re: Log on form the website

The Manual wrote:

Website Integration

Integrating PunBB into your website code is easy if you know a little PHP. By including PunBB's script common.php, you gain access to all PunBB global variables such as $db and $pun_user. However, in order to include this file, you need to define a constant called PUN_ROOT. This constant should be set to the relative path to your PunBB forum directory. For example, if your website front page is located in /home/user/public_html/ and your forums are located in /home/user/public_html/forums/, your PUN_ROOT should be './forums/'. The PHP code to accomplish this could look something like this:

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

Once you've included common.php, you can access and utilize all of PunBB's global variables and functions. Typically, you will be most interested in the $pun_user array. This array holds information about the current user. Another interesting variable is the database object $db. How to use these variables in your own script is out of the scope of this document, but here's a small example showing you how to print a simple message greeting the current user on your website front page:

Hello <?php echo pun_htmlspecialchars($pun_user['username']); ?>!

In addition to defining PUN_ROOT, you can define a number of other constants to alter the behaviour of PunBB when including common.php. The two most interesting of these constants are PUN_TURN_OFF_MAINT and PUN_QUIET_VISIT. If PUN_TURN_OFF_MAINT is defined when including common.php, PunBB will not exit and display the maintenance message if the forums are placed into maintenance mode. You generally don't want this message to appear on your website front page, so defining that constant is a good idea. The other constant, PUN_QUIET_VISIT, prevents PunBB from updating the current user's last visit data in the database so that a visit to the front page doesn't count as a visit in the forums. This is also desirable behaviour in most cases. It doesn't matter what value you set these constants to, but setting them to a value of 1 is probably a good idea. Example:

define('PUN_TURN_OFF_MAINT', 1);
define('PUN_QUIET_VISIT', 1);

Please note that these constants must be defined before common.php is included.

Re: Log on form the website

ConnorHD - I had to much trouble with that, so I just used HTML.

macius - Put this in the header right before "</head>"...It's the Javascript that prevents them from logging in without putting in a username or password.

<script type="text/javascript">
<!--
function process_form(the_form)
{
    var element_names = new Object()
    element_names["req_username"] = "Username"
    element_names["req_password"] = "Password"

    if (document.all || document.getElementById)
    {
        for (i = 0; i < the_form.length; ++i)
        {
            var elem = the_form.elements[i]
            if (elem.name && elem.name.substring(0, 4) == "req_")
            {
                if (elem.type && (elem.type=="text" || elem.type=="textarea" || elem.type=="password" || elem.type=="file") && elem.value=='')
                {
                    alert("\"" + element_names[elem.name] + "\" is a required field in this form.")
                    elem.focus()
                    return false
                }
            }
        }
    }

    return true
}
// -->
</script>

This is the login info...(I modded it for my site, mod it to match your own...)

<form id="login" method="post" action="forumroot/login.php?action=in" onsubmit="return process_form(this)">
<div class="infldset">
<input type="hidden" name="form_sent" value="1" />                    <input type="hidden" name="redirect_url" value="index.php" />
<label class="conl"><strong>Username</strong>
<br />
<input type="text" name="req_username" size="10" maxlength="25" tabindex="1" />
<br /></label>
<label class="conl"><strong>Password</strong>
<br />
<input type="password" name="req_password" size="10" maxlength="16" tabindex="2" /><br /></label>
<a href="forumroot/register.php" tabindex="4">Not registered yet?</a><br />
<a href="forumroot/login.php?action=forget" tabindex="5">Forgotten your password?</a>
<br />
<input type="submit" name="login" value="Login" tabindex="3" />
</div>
</form>

Change all instances of "forumroot" to the path of your forum.

6 (edited by Connorhd 2005-02-28 20:18)

Re: Log on form the website

erissiva: i think you need php to stop people from viewing a page while logged out

heres an example

<?php
define('PUN_ROOT', './forums/');
define('PUN_TURN_OFF_MAINT', 1);
define('PUN_QUIET_VISIT', 1);
require PUN_ROOT.'include/common.php';

if ($pun_user['is_guest'])
      exit('Sorry you must login to view this page');
?>
HTML SITE

7 (edited by erissiva 2005-02-28 20:31)

Re: Log on form the website

Connorhd wrote:

erissiva: i think you need php to stop people from viewing a page while logged out

heres an example

<?php
define('PUN_ROOT', './forums/');
define('PUN_TURN_OFF_MAINT', 1);
define('PUN_QUIET_VISIT', 1);
require PUN_ROOT.'include/common.php';

if ($pun_user['is_guest'])
      exit('Sorry you must login to view this page');
?>
HTML SITE

Oh, well I was just going for a basic login-redirect issue. All the pages on the rest of the site are done through punBB, so I don't have to worry about that. I don't want people to have to register to see the index of my site.

Edit: Oh, wait. I didn't see maicus' post about checking user rights. *ka-doh!* Sorry...
Ignore me! wink

8 (edited by macius 2005-03-03 16:49)

Re: Log on form the website

Connorhd wrote:
if ($pun_user['is_guest'])
?>

And how to check if he user is a moderator and admin?

Re: Log on form the website

$pun_user['group_id']
1 is admin 2 is mod 3 is guest 4 is member 5+ are other groups i think

10

Re: Log on form the website

Like this?

if ($pun_user['1'])

Re: Log on form the website

no
if ($pun_user['group_id']  == 1)
i think

Re: Log on form the website

1 can be replaced with PUN_ADMIN
2 with PUN_MOD

13

Re: Log on form the website

Thanks, it works.
DO you know how to redirect to my home page after logging out.

Re: Log on form the website

line 100 login.php

redirect('index.php', $lang_login['Logout redirect']);

put the url where it says index.php

15 (edited by macius 2005-03-03 18:41)

Re: Log on form the website

I want to create a logging out link.

<a href="forum/login.php?action=out&id=2">Log out</a>

Maybe there is a way to do something like here

erissiva wrote:
<input type="hidden" name="redirect_url" value="index.php" />

Edit: I have just noticed that id is different for each user, is there other way to do that?

Re: Log on form the website

<?php echo $pun_user['id'] ?> will insert the id anywhere in the page

17 (edited by macius 2005-03-04 19:49)

Re: Log on form the website

I use this code (lines 59 an 60)

if ($pun_user['group_id']  == PUN_ADMIN||$pun_user['group_id']  == PUN_MOD)
    echo("<a href=\"forum/login.php?action=out&id=$pun_user['id']\">LOG OUT</a>");

And it doesn't work:

index.php wrote:

Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in c:\usr\krasnal\www\_jgames_\index.php on line 60

Why?

Re: Log on form the website

if ($pun_user['group_id'] == PUN_ADMIN || $pun_user['group_id'] == PUN_MOD)
    echo("<a href=\"forum/login.php?action=out&id=$pun_user['id']\">LOG OUT</a>");

might work better

19 (edited by macius 2005-03-04 20:19)

Re: Log on form the website

No, still the same.

That's the whole code:

<html>
<head>
<?php
define('PUN_ROOT', './forum/');
define('PUN_TURN_OFF_MAINT', 1);
define('PUN_QUIET_VISIT', 1);
require PUN_ROOT.'include/common.php';
?>
<script type="text/javascript">
<!--
function process_form(the_form)
{
    var element_names = new Object()
    element_names["req_username"] = "Username"
    element_names["req_password"] = "Password"

    if (document.all || document.getElementById)
    {
        for (i = 0; i < the_form.length; ++i)
        {
            var elem = the_form.elements[i]
            if (elem.name && elem.name.substring(0, 4) == "req_")
            {
                if (elem.type && (elem.type=="text" || elem.type=="textarea" || elem.type=="password" || elem.type=="file") && elem.value=='')
                {
                    alert("\"" + element_names[elem.name] + "\" jest wymagane aby si? zalogowa?.")
                    elem.focus()
                    return false
                }
            }
        }
    }

    return true
}
// -->
</script>
</head>
<body>

<?php
if ($pun_user['is_guest'])
    {    
    echo('<form id="login" method="post" action="forum/login.php?action=in" onsubmit="return process_form(this)">
    <div class="infldset">
    <input type="hidden" name="form_sent" value="1" />
    <input type="hidden" name="redirect_url" value="../index.php" />
    <logowanie>Login:   </logowanie>
    <input type="text" name="req_username" size="10" maxlength="25" tabindex="1" />
    
    <logowanie>  Has?o:  </logowanie>
    <input type="password" name="req_password" size="10" maxlength="16" tabindex="2" />

    <input type="submit" name="login" value="Login" tabindex="3" />

    </form>');    
    }

if ($pun_user['group_id'] == PUN_ADMIN || $pun_user['group_id'] == PUN_MOD)
    echo("<a href=\"forum/login.php?action=out&id=$pun_user['id']\">LOG OUT</a>");
/*dla reszty*/
else
    {
    
    }
?>


</body>
</html>

Re: Log on form the website

if ($pun_user['group_id'] == PUN_ADMIN || $pun_user['group_id'] == PUN_MOD)
    echo("<a href=\"forum/login.php?action=out&id=".$pun_user['id']."\">LOG OUT</a>");

?

21

Re: Log on form the website

Yeah it works, thanks for help.

22

Re: Log on form the website

There is another problem. Although I am logged in all the time, I'm not logged in till I open the forum site.

Re: Log on form the website

what do you mean?

24 (edited by macius 2005-03-12 19:13)

Re: Log on form the website

I enabled "Save username and password between visits." option. And I have to visit the forum first to make this code work:

if ($pun_user['group_id'] == PUN_ADMIN || $pun_user['group_id'] == PUN_MOD)
    echo("<a href=\"forum/login.php?action=out&id=".$pun_user['id']."\">LOG OUT</a>");

25

Re: Log on form the website

please help me, i have a probleme when i logged me with the login script. the user has'nt updated on table ("forum_online" for me) what is the solution ??

<?php
define('PUN_ROOT', './forum/');
define('PUN_TURN_OFF_MAINT', 1);
define('PUN_QUIET_VISIT', 1);
require PUN_ROOT.'include/common.php';
if ($pun_user['group_id'] == PUN_ADMIN || $pun_user['group_id'] == PUN_MOD || $pun_user['group_id'] == PUN_MEMBER)
    echo $pun_user['username']." <a href=\"forum/login.php?action=out&id=".$pun_user['id']."\">LOG OUT</a>";
else {
?>
<script type="text/javascript">
<!--
function process_form(the_form)
{
    var element_names = new Object()
    element_names["req_username"] = "Username"
    element_names["req_password"] = "Password"

    if (document.all || document.getElementById)
    {
        for (i = 0; i < the_form.length; ++i)
        {
            var elem = the_form.elements[i]
            if (elem.name && elem.name.substring(0, 4) == "req_")
            {
                if (elem.type && (elem.type=="text" || elem.type=="textarea" || elem.type=="password" || elem.type=="file") && elem.value=='')
                {
                    alert("\"" + element_names[elem.name] + "\" is a required field in this form.")
                    elem.focus()
                    return false
                }
            }
        }
    }
}

-->
</script>
<form id="login" method="post" action="forum/login.php?action=in" onsubmit="return process_form(this)">
<div class="infldset">
<input type="hidden" name="form_sent" value="1" />                    <input type="hidden" name="redirect_url" value="index.php" />
<label class="conl"><strong>Username</strong>
<br />
<input type="text" name="req_username" size="10" maxlength="25" tabindex="1" />
<br /></label>
<label class="conl"><strong>Password</strong>
<br />
<input type="password" name="req_password" size="10" maxlength="16" tabindex="2" /><br /></label>
<a href="forum/register.php" tabindex="4">Not registered yet?</a><br />
<a href="forum/login.php?action=forget" tabindex="5">Forgotten your password?</a>
<br />
<input type="submit" name="login" value="Login" tabindex="3" />
</div>
</form>

<?php
}
?>
<br><br>

Liste des connecté(es)<br>

<?php


if ($pun_config['o_users_online'] == '1')
{
    // Fetch users online info and generate strings for output
    $num_guests = 0;
    $users = array();
    $result = $db->query('SELECT user_id, ident FROM '.$db->prefix.'online WHERE idle=0 ORDER BY ident', true) or error('Unable to fetch online list', __FILE__, __LINE__, $db->error());

    while ($pun_user_online = $db->fetch_assoc($result))
    {
        if ($pun_user_online['user_id'] > 1)
            $users[] = "<a href=\"forum/profile.php?id=".$pun_user_online['user_id']."\">".pun_htmlspecialchars($pun_user_online['ident'])."</a>";
        else
            ++$num_guests;
    }

    $num_users = count($users);

    if ($num_users > 0)
        echo implode(' - ', $users);
}

?>

Thanks you to reply me smile smile