Welcome Guest, Not a member yet? Register   Sign In
Howto make a login/auth .. thing ;)
#1

[eluser]DynamiteN[/eluser]
Hi, i have started to use Matchbox and was wondering how i should go about when i want to make .. i guess a login/member controller...

i have my main controller in "modules/home/controllers/main.php"

this is then the main controller (the default)

Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Main extends Controller {
    
    function __construct() {
        parent::Controller();
    }
    
    function index() {
        
        $data['heading'] = "Main View";
        $this->load->view('main_view', $data);
    }

}
and in the main_view file i have this to include the header
Code:
<?php $this->load->view('inc/header'); //including the header that's in "views/inc" folder ?>

and this to include the footer
Code:
<?php $this->load->view('inc/footer'); //including the footer that's in "views/inc" folder ?>

in my header i then include a file called "user_view" that also resides in the "views/inc" folder
Code:
<?php $this->load->view('inc/user_view'); ?>
just so that i can organise a little better and know whats where and such.


the thing is i just dont know where to start or how to do exactly,
i have looked at those different libraries for auth and such in the wiki.
but feel like i would benefit much more if i get to experiment and find out how do it for my self from scratch
and hopefully in the end get a app that i am satisfied with and know exactly what it does and so fourth...

if anyone has any ideas or useful links with some tutorials where i can learn more about this,
i've been looking alot on different sites and following tutorials, but there really aint that much just about this :/..

Thanks for any help at all .. Smile
#2

[eluser]Jacob Krustchinsky[/eluser]
Ill give you a quick outline, but in order to learn it entirely I would recommend http://net.tutsplus.com/tutorials/php/us...-with-php/ as an article to understand the logic and then apply it to codeigniter.

Make an auth controller and an auth model.

Auth controller:

Auth()
index()
register()
login()
logout()

This controller will handle all of the basic stuff. Utilize the form validation class to make your job easier.


Auth model:

Basic CRUD model, addUser(), editUser(), deleteUser(), getUserInfo() etc.


You could even add a library to handle some simple checks to clean up your code, like..

$this->auth_check->isAdmin() or $this->auth_check->isUser()
#3

[eluser]DynamiteN[/eluser]
Thx for replying, ye i really needed to be reminded of the logic there in that tut, as it is easy to overthink alot when trying all new stuff Tongue thx

ye i figured i would have to make some sort of a controller to handle som basic stuff, im gonna giva it a new try tomorow when i have had a good night sleep Smile

Also, i forgot to ask this question in the first post.
as i am using matchbox so i use modules i was wondering is it possible if i would be able to something like this:

-modules
--home
--auth/members
---controllers
----/login
----/register

have a complete seperate module called something like "auth" or "members" or something where all controllers about logging in -out , registering, adn all that stuff are??

and then somehow use them in the other modules such as the "home"
reason i'm wondering is that i would really like to have all that have to do with the auth part of the site in one place and all the other modules then use the function and such from that module
this is so i can be a bit orginased and i am planning to do a admin module where i then manage the whole site, and it would be nice not have to pretty much use duplicates of of controller in every module

instead i would then use the controller adn function from the auth module in the admin/backend module

hope everything made sence, as im a bit tired Smile
thankful for any ideas on that...

but will definetly try to do a small app first to experiment Smile but first some sleep Smile
thx for helping Smile
#4

[eluser]pbreit[/eluser]
You might want to check "Modular Separation" which is the successor to Matchbox. And Ion Auth is a nice, lean auth library.
#5

[eluser]DynamiteN[/eluser]
[quote author="pbreit" date="1280138918"]You might want to check "Modular Separation" which is the successor to Matchbox. And Ion Auth is a nice, lean auth library.[/quote]

i will check out Modular Separation but as fir the Ion Auth, i am trying to do my own , this way in the end i will have it the way exactly as i want Smile
#6

[eluser]pbreit[/eluser]
Yeah, I was thinking even if you roll your own, it's nice to look at a good example. Another even simpler example is QuickAuth.
#7

[eluser]DynamiteN[/eluser]
k , well i've been trying to look at some of the other auth libraries and such, but most of them i just can't seem to wrap my head around, but i will be trying everydaty until my brain spins up to the right speed Smile
not much seem to get stuck now a days,
but thx for bringing ideas as i really need 'em Smile
#8

[eluser]pbreit[/eluser]
I know exactly what you mean. And it didn't help that I put the wrong name in. I was actually referring to QuickAuth which is very simple and easy to follow: http://www.daveblencowe.com/2010/quickauth-v2-0/

Ion Auth has quite a few more features but is still pretty easy to understand.
#9

[eluser]DynamiteN[/eluser]
[quote author="pbreit" date="1280195622"]I know exactly what you mean. And it didn't help that I put the wrong name in. I was actually referring to QuickAuth which is very simple and easy to follow: http://www.daveblencowe.com/2010/quickauth-v2-0/

Ion Auth has quite a few more features but is still pretty easy to understand.[/quote]

ye , i do understand about half of it when i look at some of the code, but as soon as i'm gonna try to start my own and open up the editor just about to write something it all just freezez for me, i just can't get started on writing anything , jsut so frustrating, think i am gonna go start all over from the basics or something...
so that i actually can finish my project Wink
#10

[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 Smile
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"> &lt;?php echo $this->session->userdata('username'); ?&gt;</a>        
    <a href="site/logout" class="logout"> LOGOUT </a>
       </div>
&lt;?php else: ?&gt;
<div id="guest">
    <a href="site/showlogin" class="login"> LOGIN </a>
</div>
&lt;?php endif; ?&gt;

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 Smile

P.S i also tried to have the session stored in database but no change in the result, works exactly the same ...




Theme © iAndrew 2016 - Forum software by © MyBB