[eluser]vecima[/eluser]
Thanks for sharing this Library. It gave me the foothold I needed to do what I wanted to do, which was to use the phpBB3 user/session/login system on my main site.
I made only slight modifications to your library, adding a login / logout method. You could just as easily utilize the phpBB3 login / logout methods, except that if you are running locally, (localhost) you need to be sure that phpBB3 is set to use that as it's cookie domain. If you install phpBB3 to your localhost it's probably fine, but if not (I copied my live forum down to my xampplite) then you need to set it.
I added these 2 methods to the library:
Code:
function logout()
{
global $user;
$user->session_kill();
$user->session_begin();
}
function login($username, $password, $autologin, $admin)
{
// use this only for localhost!!
global $config;
$config['cookie_domain'] = 'localhost';
global $auth;
// Attempt authorization. result can be used to send messages or perform
// loggin success / fail logic.
$result = $auth->login($username, $password, $autologin, true, $admin);
}
notice in the login function that the lines with $config are only needed in the case of having a forum on localhost, with it's cookie_domain set wrong (you can test this by logging into the forum on localhost, then check your cookies to see if there are any phpbb3 cookies under "localhost").
in my base controller (that all others inherit from) I defined login and logout methods to use CI form validation:
Code:
function login($admin = FALSE)
{
//validate the form input
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
$this->form_validation->set_rules('username', 'Username', 'required|xss_clean|prep_for_form|encode_php_tags');
$this->form_validation->set_rules('password', 'Password', 'required|xss_clean|prep_for_form|encode_php_tags');
$this->form_validation->set_rules('redirect', 'Redirect', 'required|xss_clean|prep_for_form|encode_php_tags');
if ($this->form_validation->run() == FALSE)
{
ci_redirect($this->input->post('redirect', TRUE));
}
else
{
$username = $this->input->post('username', TRUE);
$password = $this->input->post('password', TRUE);
$autologin = $this->input->post('autologin', TRUE);
$this->phpbb_ci_auth->login($username, $password, $autologin, $admin);
ci_redirect($this->input->post('redirect', TRUE));
}
}
function logout()
{
//validate the form input
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
$this->form_validation->set_rules('redirect', 'Redirect', 'required|xss_clean|prep_for_form|encode_php_tags');
if ($this->form_validation->run() == FALSE)
{
ci_redirect($this->input->post('redirect', TRUE));
}
else
{
$this->phpbb_ci_auth->logout();
ci_redirect($this->input->post('redirect', TRUE));
}
}
and my login view that's shown if you're not logged in has a form:
Code:
<form action="<?=base_url();?>index.php/<?=$controller;?>/login" method="post">
<a href="<?=base_url();?>index.php/<?=$controller;?>/login">Login</a> • <a href="<?=base_url();?>PHPBB_FORUM_PATH/ucp.php?mode=register">Register</a>
<fieldset>
<?php echo form_error('redirect'); ?>
<?php echo form_error('username'); ?>
<label for="username">Username:</label>
<input type="text" name="username" id="username" size="10" title="Username" />
<?php echo form_error('password'); ?>
<label for="password">Password:</label>
<input type="password" name="password" id="password" size="10" title="Password" />
<label for="autologin">Log me on automatically each visit <input type="checkbox" name="autologin" id="autologin" /></label>
<input type="hidden" name="redirect" value="<?=base_url();?>index.php/<?=$controller;?>" />
<input type="submit" name="login" value="Login" />
</fieldset>
</form>
hopefully I was clear in my description. If not feel free to ask questions. Also feel free to call me out on "crap code" and make a better version - I just wanted it to work, but if you can do it better, by all means show us how!