Welcome Guest, Not a member yet? Register   Sign In
Problem with access
#1

[eluser]elmne[/eluser]
I have the following controller in the admin folder

Code:
<?php

class Homepage extends Controller {

    function __construct()
    {
        parent::Controller();    
        $this->load->library('auth');
    }
    
    function index()
    {
        $this->load->view('layout/admin/main');        
    }
    
    
    function login()
    {
        if ($this->input->post('submit') != FALSE)
        {
            $login = array($this->input->post('username'), $this->input->post('password'));
            if($this->auth->process_login($login))
            {
                // Login successful, then redirect.
                $this->auth->redirect();
            }
            else
            {
                $data['error'] = 'Login failed, please try again';
                $this->load->vars($data);
            }
        }
        $this->load->view('layout/admin/login');
    }
    

}

I then have this in the library as the Admin_controller

Code:
<?php


class Admin_Controller extends MY_Controller
{
    function __construct()
    {
            parent::__construct();
            
            if($this->data['user']['group'] !== 'admin')
            {
            show_error('This is administrator area only.');
            }
    }
}




?>

I then have this as the auth.php within teh library too

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

class Auth {

    var $CI = null;
    
    
     // get the CI Super object
    function __construct(){
        $this->CI =& get_instance();
    }


    function Auth()
    {
        $this->CI =& get_instance();

        $this->load->library('session');
        $this->load->database();
        $this->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];
    
        // Query time
        $this->db->where('username', $username);
        $this->db->where('password', $password);
        $query = $thisdb->get('user_account_staff');
    
        if ($query->num_rows() == 1)
        {
            // Our user exists, set session.
            $this->CI->session->set_userdata('logged_user', $username);
            return TRUE;
        }
        else
        {
            // No existing user.
            return FALSE;
        }
    }
    
    
    function redirect()
    {
        if ($this->CI->session->userdata('redirected_from') == FALSE)
        {
            redirect('/admin','', 301);
        } else {
            redirect($this->CI->session->userdata('redirected_from'));
        }
    
    }

    

    /**
     *
     * This function restricts users from certain pages.
     * use restrict(TRUE) if a user can't access a page when logged in
     *
     * @access    public
     * @param    boolean    wether the page is viewable when logged in
     * @return    void
     */
    function restrict($logged_out = FALSE)
    {
        // If the user is logged in and he's trying to access a page
        // he's not allowed to see when logged in,
        // redirect him to the index!
        if ($logged_out && $this->logged_in())
        {
            redirect('/admin');
        }
    
        // If the user isn' logged in and he's trying to access a page
        // he's not allowed to see when logged out,
        // redirect him to the login page!
        if ( ! $logged_out && ! $this->logged_in())
        {
            $this->CI->session->set_userdata('redirected_from', $this->CI->uri->uri_string()); // We'll use this in our redirect method.
            redirect('/admin/login');
        }
    }
    
    /**
     *
     * Checks if a user is logged in
     *
     * @access    public
     * @return    boolean
     */
    function logged_in()
    {
        if ($this->CI->session->userdata('logged_user') == FALSE)
        {
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }

    


}

I then have a login form like this

Code:
<?php echo form_open('admin/login'); ?>
      <label><br />
      Username
      &lt;input type="text" name="username" value="" id="username"/&gt;
      </label>
      <p>
        <label>Password
        &lt;input type="password" name="password" value="" id="passw"  /&gt;
        </label>
        </p>
      <p>
        
        &lt;input type="submit" name="submit" value="Login" /&gt;
</p>
    &lt;/form&gt;
#2

[eluser]elmne[/eluser]
But when i try to access login using the path "admin/login"

the form just loops to the homepage as if the login controller was not found.

What causes this?


I happen to have subdirectories within admin, but there is an extended router in the library which first detects controllers then checks for folders, so i don't think it's the cause.

The controller is shown below

Code:
&lt;?php

/*
* Custom router function v 0.1
*
* Add functionality : read into more than one sub-folder
*
*/

Class MY_Router extends CI_Router
{
    Function MY_Router()
    {
        parent::CI_Router();
    }

    function _validate_request($segments)
    {
        if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
        {
            return $segments;
        }

        if (is_dir(APPPATH.'controllers/'.$segments[0]))
        {
            $this->set_directory($segments[0]);
            $segments = array_slice($segments, 1);

            /* ----------- ADDED CODE ------------ */

            while(count($segments) > 0 && is_dir(APPPATH.'controllers/'.$this->directory.$segments[0]))
            {
                // Set the directory and remove it from the segment array
            $this->set_directory($this->directory . $segments[0]);
            $segments = array_slice($segments, 1);
            }

            /* ----------- END ------------ */

            if (count($segments) > 0)
            {
                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
                {
                    show_404($this->fetch_directory().$segments[0]);
                }
            }
            else
            {
                $this->set_class($this->default_controller);
                $this->set_method('index');

                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
                {
                    $this->directory = '';
                    return array();
                }

            }

            return $segments;
        }

        show_404($segments[0]);
    }
}

?&gt;

Could someone please advise why validation is failing and instead of directing to the admin index page, the homepage is loaded?
#3

[eluser]pickupman[/eluser]
It's hard to tell b/c you posted everything except your admin controller? Looks like homepage index is loading sme sort of admin view? Is your browser redirecting when accessing the url, or are you meaning when your submitting the form?
#4

[eluser]elmne[/eluser]
This is the admin controller, i think i included it above. "admin" is a folder, so when the folder is accessed, homepage.php is set as the default controller to call up.

Code:
&lt;?php

class Homepage extends Controller {

    function __construct()
    {
        parent::Controller();    
        $this->load->library('auth');
    }
    
    function index()
    {
      
        //a data array of values
        $data = array(
          'title' => 'Admin',
        'currentpage' => 'System Administration',
        'content' => 'This is the system Administration'
         );
        
        // The view is then loaded and data is passed from controller to the view using the second parameter
        $this->load->view('layout/admin/main',$data);        
    }
    
    
    function login()
    {
        if ($this->input->post('submit') != FALSE)
        {
            $login = array($this->input->post('username'), $this->input->post('password'));
            if($this->auth->process_login($login))
            {
                // Login successful, then redirect.
                $this->auth->redirect();
            }
            else
            {
                $data['error'] = 'Login failed, please try again';
                $this->load->vars($data);
            }
        }
        $this->load->view('layout/admin/login');
    }


    
    

}



/* End of file homepage.php */
/* Location: ./system/application/controllers/admin/homepage.php */
#5

[eluser]pickupman[/eluser]
Maybe a typo
Code:
$query = $thisdb->get('user_account_staff');

//To
$query = $this->db->get('user_account_staff');

If that's how you are using a MY_Router.php, shouldn't you be accessing /admin/homepage/login and not admin/login or are you rewriting that url in routes.php?
#6

[eluser]elmne[/eluser]
I corrected the typo error. What i get now when i access as you said "admin/homepage/login" is this

Quote:A PHP Error was encountered
Severity: Notice

Message: Undefined property: Homepage::$session

Filename: libraries/auth.php

Line Number: 103


Fatal error: Call to a member function userdata() on a non-object

But session is loaded in


Code:
function Auth()
    {
        $this->CI->load->library('session');
        $this->CI->load->database();
        $this->CI->load->helper('url');
    }


and line 103 is this: if ($this->CI->session->userdata('logged_user') == FALSE)

Code:
/**
     *
     * Checks if a user is logged in
     *
     * @access    public
     * @return    boolean
     */
    function logged_in()
    {
        if ($this->CI->session->userdata('logged_user') == FALSE)
        {
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }
#7

[eluser]pickupman[/eluser]
You have different versions of your Auth.php constructor. You have both function Auth() and __construct(). I would move all the code into one spot or another.
Code:
class Auth{
  
   var $CI; //declare property

   function Auth()
    {
        $this->CI =& get_instance();
        $this->CI->load->library('session');
        $this->CI->load->database();
        $this->CI->load->helper('url');
    }
}


// or //
class Auth{
  
   var $CI; //declare property

   function __construct()
    {
        $this->CI =& get_instance();
        $this->CI->load->library('session');
        $this->CI->load->database();
        $this->CI->load->helper('url');
    }
}

Also, what about autoloading the session class, and having it on for everything?
#8

[eluser]elmne[/eluser]
I have made the changes and set session to autoload

The problem still exists, the page still gets redirected to homepage.

Is there a way to view output line of code during processing that leads to re-direction?
#9

[eluser]pickupman[/eluser]
Well, you can open your config.php file and bump up the logging value. This will add all messages during CI that are called, and put in your system/logs folder.




Theme © iAndrew 2016 - Forum software by © MyBB