[eluser]DynamiteN[/eluser]
okey, so now i have managed to get somewhere i trhew out the modules for now so that i can focus on this login auth thing
so i found a really easy tut over at bramme.net, and stripped some of it so that i can make it my own and build on it
libraries/auth.php :
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Auth {
var $CI = NULL;
function __construct() {
$this->CI =& get_instance();
$this->CI->load->library('session');
$this->CI->load->database();
$this->CI->load->helper('url');
}
function process_login($login = NULL) {
// A few safety checks
// Our array has to be set
if(!isset($login))
return FALSE;
//Our array has to have 2 values
//No more, no less!
if(count($login) != 2)
return FALSE;
$username = $login[0];
$password = $login[1];
if($this->CI->member_model->checkLogin($username, $password)){
$this->CI->session->set_userdata('logged_user', $username);
return TRUE;
}
}
function logged_in() {
if ($this->CI->session->userdata('logged_user') == FALSE) {
return FALSE;
}else{
return TRUE;
}
}
/**
* Logs user out by destroying the session.
*/
function logout()
{
$this->CI->session->sess_destroy();
return TRUE;
}
}
/* End of file: Auth.php */
/* Location: ./system/application/libraries/Auth.php */
and the member_model that does the query
models/member_model.php :
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); ?>
<?php
/**
* Ci Model Class(Constructor)
*
* @package default
* @author DynamiteN
**/
class Member_model extends Model {
function __construct(){
parent::Model();
}
function checklogin($username, $password) {
// Query time
$this->db->where('username', $username);
$this->db->where('password', $password);
$query = $this->db->get('members');
if ($query->num_rows() == 1) {
// Our user exists, set session.
return TRUE;
}else{
// No existing user.
return FALSE;
}
}
}
and then i put this in my controller
controllers/site.php :
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Site extends Controller {
function __construct() {
parent::Controller();
$this->auth->logged_in();
}
function index() {
$data['heading'] = "Main View";
$this->load->view('main_view', $data);
}
function showlogin() {
$data['heading'] = "Login";
$this->load->view('login_view', $data);
}
function login() {
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
if ($this->form_validation->run() == FALSE) {
$data['heading'] = "Login";
$this->load->view('login_view', $data);
}else{
$login = array(
$this->input->post('username'),
md5($this->input->post('password'))
);
if($this->auth->process_login($login)) {
$this->index();
}
}
}
function logout() {
if($this->auth->logout())
redirect('/site/login');
}
}
and it works... sort of...
when i go login enter username and password it processes it and i go back to the index function (first page)
and in my header view i have put this
Code:
<?php if($this->auth->logged_in()) : ?>
<div id="user">
<a href="#username" class="username"> <?php echo $this->session->userdata('username'); ?></a>
<a href="site/logout" class="logout"> LOGOUT </a>
</div>
<?php else: ?>
<div id="guest">
<a href="site/showlogin" class="login"> LOGIN </a>
</div>
<?php endif; ?>
the problem is that i cant get it to show the username of the logged in person.
When i am logged out i only see the login link.
And when i am logged in i se my logout button, but i do not know what to do to make the username and stuff show :/
any help on this would be really helpful
P.S i also tried to have the session stored in database but no change in the result, works exactly the same ...