Welcome Guest, Not a member yet? Register   Sign In
Fresh Powered - Auth Library
#31

[eluser]Adam Griffiths[/eluser]
To log a user in, you only need to call upon one function.

Code:
$this->auth->login();

That's the only thing you need inside your controller to log somebody in. This is the same for the register function.

To restrict access to a controller, you put the restrict function inside the controller function.


All of this is documented in the user guide, link in my signature.
#32

[eluser]BrentNJ[/eluser]
Thank you for your patience.

This is what I have

<?php

class Contacts extends Controller {
function Contacts()
{
parent::Controller();

$this->load->helper('url');
$this->load->helper('form');
$this->load->library('auth'); // Loading the auth library
$this->auth->function(); // How to use a function in the auth library

}

function index()
{
$data['query'] = $this->db->get('contacts');
$this->auth->login();
$this->load->view('contacts_view',$data);
}


When I added the
$this->load->library('auth'); // Loading the auth library
$this->auth->function(); // How to use a function in the auth library

The page now shows a blank - not even the html or body tags
#33

[eluser]Adam Griffiths[/eluser]
Where you have $this->auth->function(); it is likely erroring here, remove it because it has no relevance to the library, there isn't a function called function.
#34

[eluser]BrentNJ[/eluser]
Hi,

This is my part of my controllers/user.php view

Code:
<?php

class User extends Controller {
    function User()
       {
            parent::Controller();
            
            $this->load->helper('url');
            $this->load->helper('form');
            $this->load->library('auth'); // Loading the auth library
            //$this->auth->function(); // How to use a function in the auth library
            
       }
      
    function index()
    {
        $data['query'] = $this->db->get('users');
        $this->load->view('user_view',$data);
    }
    
    function login()
    {
        $this->load->view('login');
    }

This is the auth.php - tried a couple of APPATHs
Code:
/**
* The default URI string to redirect to after a successful login.
*/
$config['auth_login'] = 'user/login';

/**
* The view to your views folder from index.php
*/
//$config['auth_view_path'] = APPPATH.'views/';

//$config['auth_view_path'] = './system/application/views/';  
$config['auth_view_path'] = 'views/';


This contacts.php

<?php

Code:
class Contacts extends Controller {
    function Contacts()
       {
            parent::Controller();
            
            $this->load->helper('url');
            $this->load->helper('form');
            $this->load->library('auth'); // Loading the auth library
            //$this->auth->function(); // How to use a function in the auth library
            
       }
      
    function index()
    {
       $data['query'] = $this->db->get('contacts');
       $this->auth->login();
       $this->load->view('contacts_view',$data);
    }


I double checked that database.

Probably something basic ;-)
#35

[eluser]Adam Griffiths[/eluser]
Sorry, I noticed a bug in my code. If you download the new set of files from here you shouldn't have any problems.
#36

[eluser]BrentNJ[/eluser]
Hi,

Started from scratch.

welcome.php worked then I loaded the library

Code:
<?php

class Welcome extends Controller {

    function Welcome()
    {
        parent::Controller();
        $this->load->library('auth');
    }
    
    function index()
    {
       // $this->auth->login();
        $this->load->view('welcome_message');
    }
}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */


Then I got an error.

I added a print_r and get

Code:
Array
(
    [auth_groups] => Array
        (
            [admin] => 1
            [editor] => 2
            [user] => 100
        )

    [auth_login] => user/login
    [auth_view_path] => c:\wamp\www\ci/system/application/views/
    [user_min_length] => 4
    [user_max_length] => 12
    [pass_min_length] => 5
    [pass_max_length] => 12
)


Fatal error: Undefined class name 'self' in c:\wamp\www\ci\system\application\libraries\Auth.php on line 50

php 4.4.7

mysql 5.0.45

This is on my pc. Most work though is on FreeBSD.
#37

[eluser]Adam Griffiths[/eluser]
I take it you are using PHP4?

Code:
/**
    * Auth constructor PHP4 support
    *
    * @access public
    * @param string
    */
    function Auth($config)
    {
        self::__construct($config);
    }

Try changing the above code in Auth.php to...

Code:
/**
    * Auth constructor PHP4 support
    *
    * @access public
    * @param string
    */
    function Auth()
    {
        self::__construct($config);
    }

If that doesn't work change it to...

Code:
/**
    * Auth constructor PHP5
    *
    * @access public
    * @param string
    */
    function Auth($config)
    {
        $this->CI =& get_instance();
        $this->CI->load->library('session');
        $this->CI->load->helper('url');
        $this->CI->load->database();
        
        $this->config = $config;
    } // Auth()

Let me know how you get on.
#38

[eluser]Julien Desrosiers[/eluser]
@adam Griffiths

I just wanted to say thank you for the great Auth library. It's so easy to setup and it has just want i need.

Thanks and good luck with your CMS.

Julien
#39

[eluser]BrentNJ[/eluser]
Hi,

used your second suggestion and can login

tested with

Code:
<?php

class Welcome extends Controller {

    function Welcome()
    {
        parent::Controller();
        $this->load->helper('url');
        $this->load->helper('form');
        $this->load->library('auth');
    }
    
    function index()
    {
        if ($this->auth->logged_in()) {
        echo "logged in";
        $this->load->view('welcome_message');    
        }
        else {
        $this->auth->login();
        }

side question - I autoload session and load url in the controller. Is that ok that it loads again in Auth.php?


Thanks for all the help!!
#40

[eluser]Julien Desrosiers[/eluser]
Hi,

I don't know if this is something that i made wrong, but i was not able to log in a section that i had restricted with the restrict() method. I got it to work by replacing the === by a == in the if/elseif statements, like this (in libraries/Auth.php) :

Code:
function restrict($group = NULL)
    {
        if($group === NULL)
        {
            if($this->CI->session->userdata('logged_in') == TRUE) // ==, not ===
            {
                return TRUE;
            }
            else
            {
                show_error('You do not have sufficient privileges to access this page.');
            }
        }
        elseif($this->CI->session->userdata('logged_in') == TRUE) // ==, not ===
        {
            
            $level = $this->config['auth_groups'][$group];
            $set_level = $this->CI->session->userdata('group');
            if($set_level <= $level)
            {
                return TRUE;
            }
            else
            {
                show_error('You do not have sufficient privileges to access this page.');
            }
        }
        else
        {
            redirect($this->config['auth_login'], 'refresh');
        }
    } // restrict

Maybe it has something to do with the database that i use (MySQL 5.0.5)?

But now it works like a charm Smile




Theme © iAndrew 2016 - Forum software by © MyBB