|
Автор |
Сообщение |
den
Старожил
Зарегистрирован: 31.01.2006 Сообщения: 13870 Откуда: Кировоград, Украина
|
|
|
|
Задача следующая. Когда юзер заходит на сайт, то выдавать приветствие типа Привет, юзер! Это в случае, если юзер зарегистрирован на форуме. Если он не зарегистрирован, то выводить форму регистрации
Решается с помощью следующего кода:
<?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['user_active']==1) // Если вы зарегистрированны то ваш ник опрделиться, иначе выдать форму регистрации
{
print "Привет, $userdata[username]";
// include($phpbb_root_path . 'includes/page_tail.'.$phpEx);
}
else
{
print "ЗАРЕГИСТРИРУЙТЕСЬ!";
print" <form method='post' action='phpbb2/login.php'>
Логин: <input type='text' name='username' size='15' id=inp>
Пароль: <input type='password' name='password' size='15' maxlength='32' id=inp>
Запомнить: <input type='checkbox' name='autologin' id=inp>
<input type='hidden' name='redirect' value='../3.php?$QUERY_STRING'> <input type='submit' name='login' value='Войти' id=fix_w2>
<a href='phpbb/profile.php?mode=register'>Регистрация</a>
<a href='phpbb/profile.php?mode=sendpassword'>Забыли пароль? Логин?</a> ";
}
?> |
|
Вернуться к началу |
dhsilabs@jabber.ru |
|
|
pengo
Новенький
Зарегистрирован: 02.06.2006 Сообщения: 39 Откуда: Kiev
|
|
|
|
спасибо, будем знать. |
|
Вернуться к началу |
|
|
den
Старожил
Зарегистрирован: 31.01.2006 Сообщения: 13870 Откуда: Кировоград, Украина
|
|
|
|
Еще нашел на эту тему хороший ресурс, но на English:
http://www.phpbb.com/kb/article/phpbb2-sessions-integration
Many people ask how they can use the phpBB2 login in the rest of their site, allowing them to protect pages for members. This can easily be achieved by using the phpBB sessions system with the rest of your site.
==First Things First==
Before you integrate any pages, you must first adjust the cookie path settings for your site. Unless you have changed it, phpBB cookies are only sent when one of your visitors is viewing your forum, not when they are looking at the rest of your site. Go to your admin control panel and change the cookie path (not script path!) setting to "/". After that, log out of your forum and delete your browser's cache and the cookies for your site. You are now ready to integrate the rest of your site!
==Required Code==
On every page you wish to integrate the phpBB sessions into, the page must first begin with the following code:
Код: |
<?php
define('IN_PHPBB', true);
$phpbb_root_path = './forum/';
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
//
?> |
However, this code must be modified to work on your site. In the second line, the $phpbb_root_path variable must be changed to point to where your forum is located. For example, if your forum is located in a folder named "phpBB", the path would be "./phpBB/". The "./" at the beginning of the path is very important, as it helps secure the page from hackers.
==What can I do now?==
Now it is possible for you to use all the phpBB system variables, such as the $userdata variable, in your own PHP scripts. Here is a basic example:
Код: |
<?php
define('IN_PHPBB', true);
$phpbb_root_path = './forum/';
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['user_logged_in'])
{
echo('Hi ' . $userdata['username'] . '!');
}
else
{
echo('You are a guest');
}
?> |
You also have access to the $db and $template variables, as well as a couple more. Experiment and feel free to post questions in the MOD authors forum.
==How can I login from a page I made?==
Logging in from another page on your site is very easy. Here is a template for a login form:
Code: Select All
<form action="forum/login.php" method="post" enctype="multipart/form-data">
<input type="text" name="username"><br />
<input type="password" name="password"><br />
<input type="hidden" name="redirect" value="../">
<input type="submit" value="login" name="login">
</form>
Again, The code must be modified to work. The form action must reflect where the forum is actually located so it can reach login.php. In this case, it is in a folder called "forum". The redirect field is optional, but if present with change where your users are directed after logging in. This is relative to where your forum is, so setting it to "mypage.php" will direct users to "www.example.com/forum/mypage.php". In the example above it is set to "../", which will direct users to the parent directory, http://www.example.com if the forum is at http://www.example.com/forum.
==How Do I make a logout Link?==
The logout URL has the general format of "login.php?logout=true&sid=0123456789abcdef0123456789abcdef". Notice how it includes the session ID of the user. While it is possible to add this to the link using $userdata['session_id'] variable, it can be added automatically using the append_sid() function. I won't go into hte details of using this function here, since there is a very good KB article explaining it Here.
Below is an example script that displays a valid logout link for logged in users.
Code: Select All
Цитата: |
<?php
define('IN_PHPBB', true);
$phpbb_root_path = './forum/';
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['user_logged_in'])
{
echo('<a href="' . append_sid($phpbb_root_path . 'login.php?logout=true') . '">Logout</a>');
}
else
{
echo('You are a guest');
}
?> |
You can also change where users are redirected when after they logout by adding "&redirect=newpage.php" to the url. Like the login reirect, it also is relative to the forum root.
Below is example code you can use as a template for your own pages. Feel free to modify it and use it as you wish.
Код: |
<?php
define('IN_PHPBB', true);
$phpbb_root_path = './forum/';
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
//
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Example sessions page</title>
<body>
<?php
if($userdata['user_logged_in'])
{
echo('Hi '.$userdata['username'].'! <a href="' . append_sid($phpbb_root_path . 'login.php?logout=true&redirect=..%2F') . '">Logout</a>');
}
else
{
?>
Hi Guest!<br />
<form action="<?php echo($phpbb_root_path); ?>login.php">
Username: <input type="text" name="username"><br />
Password: <input type="password" name="password"><br />
<input type="hidden" name="redirect" value="../">
<input type="submit" value="login" name="login">
</form>
<?php
}
?>
</body>
</html> |
|
|
Вернуться к началу |
dhsilabs@jabber.ru |
|
|
den
Старожил
Зарегистрирован: 31.01.2006 Сообщения: 13870 Откуда: Кировоград, Украина
|
|
|
|
pengo писал(а): |
спасибо, будем знать. |
Не за что. Я сам как раз разбирался с этим, думаю выложу на форум |
|
Вернуться к началу |
dhsilabs@jabber.ru |
|
|
alex2103
Новенький
Зарегистрирован: 15.10.2008 Сообщения: 57
|
|
|
|
Привет!!!
Подскажите в чем может быть проблема, я все сделал как описано выше - авторизация работает а вот выход не работает
if($userdata['user_logged_in'])
{
echo('Hi '.$userdata['username'].'! <a href="' . append_sid($phpbb_root_path . 'login.php?logout=true&redirect=..%2F') . '">Logout</a>');
}
Спасибо! |
|
Вернуться к началу |
|
|
alex2103
Новенький
Зарегистрирован: 15.10.2008 Сообщения: 57
|
|
|
|
пишет
Invalid_session |
|
Вернуться к началу |
|
|
den
Старожил
Зарегистрирован: 31.01.2006 Сообщения: 13870 Откуда: Кировоград, Украина
|
|
|
|
alex2103, для создания кнопки Выход сделай просто ссылку на
/phpbb2/login.php?logout=true |
|
Вернуться к началу |
dhsilabs@jabber.ru |
|
|
alex2103
Новенький
Зарегистрирован: 15.10.2008 Сообщения: 57
|
|
|
|
тоже самое пишет
Invalid_session |
|
Вернуться к началу |
|
|
den
Старожил
Зарегистрирован: 31.01.2006 Сообщения: 13870 Откуда: Кировоград, Украина
|
|
|
|
Тогда так
"/phpbb2/login.php?logout=true&sid=". SID; |
|
Вернуться к началу |
dhsilabs@jabber.ru |
|
|
alex2103
Новенький
Зарегистрирован: 15.10.2008 Сообщения: 57
|
|
|
|
вот код, может быть проблема в настройке сервера или php?
<?php if($userdata['session_logged_in'])
{
echo('Доброго времени суток <strong>'.$userdata['username'].'!</strong><br><br>');
echo '<a href="/forum/login.php?logout=true&sid=".SID>Logout</a>';
}
else
{
echo('Доброго времени суток незнакомец!');
?> |
|
Вернуться к началу |
|
|
den
Старожил
Зарегистрирован: 31.01.2006 Сообщения: 13870 Откуда: Кировоград, Украина
|
|
|
|
По идее должо работать... |
|
Вернуться к началу |
dhsilabs@jabber.ru |
|
|
alex2103
Новенький
Зарегистрирован: 15.10.2008 Сообщения: 57
|
|
|
|
я решил так проблему
<a href="/forum/login.php?logout=true&sid='.$userdata['session_id'].'&redirect=..%2F">Выход</a> |
|
Вернуться к началу |
|
|
den
Старожил
Зарегистрирован: 31.01.2006 Сообщения: 13870 Откуда: Кировоград, Украина
|
|
|
|
Отлично Вот только не пойму, почему у тебя константа SID не работала. |
|
Вернуться к началу |
dhsilabs@jabber.ru |
|
|
Akito
Зарегистрирован: 20.03.2009 Сообщения: 7
|
|
|
|
В прошлой темке не ответили, напишу в этой может ответят
Проблемки в разбирательстве с книжкой Движок для вашего сайта.
Возникла проблема с полным отказом функционирования сайта(ни меню ни страницы из статика не работают) В ходе выяснения обстоятельств оказалось проблема именно в распозновании пользователей
ob_start();
define('IN_PHPBB', true);
$phpbb_root_path = 'forum/';
include($phpbb_root_path . 'extension.inc');
include($phpbb_root_path . 'common.'.$phpEx);
include_once "forum/config.php";
$userdata = session_pagestart($user_ip, PAGE_INDEX);
init_userprefs($userdata);
if ($userdata['user_active']==1)
$reg = "Добро пожаловать, $userdata[username]";
else
$reg = join('',file('auth.html'));
$tpl->set_value('REG',$reg);
ob_end_flush();
Если выделенные строчки закоментировать то все оживает, но сайт естественно не узнает пользователей.
Есть предположения что виновато использования локального сервера(тобиш Денвера) так или не так?
Есть еще пару вопросов ппро грабер(если вдруг начнут отвечать) |
|
Вернуться к началу |
|
|
den
Старожил
Зарегистрирован: 31.01.2006 Сообщения: 13870 Откуда: Кировоград, Украина
|
|
|
|
Постучись ко мне в асю ближе к 3 часам по киевскому времени - вместе посмотрим. |
|
Вернуться к началу |
dhsilabs@jabber.ru |
|
|
|
|