Welcome Guest, Not a member yet? Register   Sign In
Creating a new lib
#1

[eluser]snowstar[/eluser]
Hi all,

im trying to create a new lib which will auto load each time a controller is loaded and authenticate to see if a user is logged in

I'm auto loading the script and its loading fine how-ever it doesn't seam to authenticate

My Lib
Code:
class Authentication {
    
    var $CI;
    function Authenication() {
        
        $this->CI =& get_instance();
            
        $this->CI->load->library('session');
        $is_logged_in = $this->CI->session->userdata('is_logged_in');
        if(!isset($is_logged_in) || $is_logged_in != true)
        {
            echo 'You don\'t have permission to access this page. <a href="../login">Login</a>';    
            die();        
        }
    }
    
}


Any suggestions are greatly appreciated
#2

[eluser]danmontgomery[/eluser]
use __construct(), not Authentication()
#3

[eluser]John_Betong_002[/eluser]
was the "Authentication()" deliberately intentional?

Code:
function Authenication() {
&nbsp;
&nbsp;
&nbsp;
#4

[eluser]snowstar[/eluser]
[quote author="noctrum" date="1308237339"]use __construct(), not Authentication()[/quote]


Thanks for the reply,

when i use __construct() i get this error:

Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: CI_Loader::$session

Filename: libraries/Authentication.php

Line Number: 11

Fatal error: Call to a member function userdata() on a non-object in /Applications/MAMP/htdocs/mysite/application/libraries/Authentication.php on line 11

i assume i linking it wrong although everything ive seen tells me to do it that way
#5

[eluser]John_Betong_002[/eluser]
I have just tried and tested this in an application and it works fine:


Code:
// FILE:  ./your_application/controllers/your_controller.php
function index()
{
  error_reporting(-1);
  ini_set('display_errors',1);

  $this->load->library('Authentication');
  die;




// FILE:  ./your_application/libraries/Authentication.php
&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Authentication {
    
// var $CI;

function __construct()
{
  
  $this->CI =& get_instance();
      
  $this->CI->load->library('session');
  $is_logged_in = $this->CI->session->userdata('is_logged_in');
  if(!isset($is_logged_in) || $is_logged_in != true)
  {
    echo 'You don\'t have permission to access this page. <a href="../login">Login</a>';    
    die();        
  }
}
}



// Output: You don't have permission to access this page. Login
&nbsp;
&nbsp;
#6

[eluser]snowstar[/eluser]
Thank you for you help, i see what i did wrong. Works perfectly!

Thanks Again

[quote author="John_Betong_002" date="1308289365"]I have just tried and tested this in an application and it works fine:


Code:
// FILE:  ./your_application/controllers/your_controller.php
function index()
{
  error_reporting(-1);
  ini_set('display_errors',1);

  $this->load->library('Authentication');
  die;




// FILE:  ./your_application/libraries/Authentication.php
&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Authentication {
    
// var $CI;

function __construct()
{
  
  $this->CI =& get_instance();
      
  $this->CI->load->library('session');
  $is_logged_in = $this->CI->session->userdata('is_logged_in');
  if(!isset($is_logged_in) || $is_logged_in != true)
  {
    echo 'You don\'t have permission to access this page. <a href="../login">Login</a>';    
    die();        
  }
}
}



// Output: You don't have permission to access this page. Login
&nbsp;
&nbsp;[/quote]
#7

[eluser]John_Betong_002[/eluser]
Quote:Thank you for you help, i see what i did wrong. Works perfectly!

So what did you do wrong?

Please mark the post as [SOLVED] to help others when searching for a similar solution.
&nbsp;
&nbsp;




Theme © iAndrew 2016 - Forum software by © MyBB