Welcome Guest, Not a member yet? Register   Sign In
New to OOP, silly question?
#1

[eluser]harmstra[/eluser]
Created my first controller, was pretty easy.
But now I am trying to create al login screen.

function index()
{
if ($this->session->userdata(’auth’) != 1)
{
redirect(’/login/’, ‘location’);
}
rest of the code goes here.....

$this->load->view(’v_usn’,$data);

}

this works fine, but the loginscreen only jumps in when i call the index function.
This controller has more functions, all of them need to be behind the login screen.

So i did some PHP5 OOP reading, and found out about the __construct function

Changed the code to:
function __construct() {

if ($this->session->userdata(’auth’) != 1)
{
redirect(’/login/’, ‘location’);
}
}

a constructor executes every time when a controller is executed, so all the functions inside this controller should be protected right?

But now i have this error:
Undefined property: Usn::$session

mm, declare the session var first?

just above the constructor i’ve put:

public $session = ‘’;

An error again:
Call to a member function userdata() on a non-objec

Now I’m really stuck. What’s wrong here?

Regards,
Harmstra
#2

[eluser]adamp1[/eluser]
Undefined property: Usn::$session

This means you have not loaded the session library. By default CI only loads what it needs. So to use the method you want to store login infomation you need to load it, either in the autoload.php config. Or at the top of your controller with
Code:
$this->load->library('session');

Some information about how your going about making your login. I would advise you too look at this thread since he had the same problem as you, and I've given some example code how best to solve it. (Sorry but I can't be bothered to retype it.)

Hope this helps.
#3

[eluser]harmstra[/eluser]
adamp1 , thanks for the reply,
i'll read that thread.
But for now, i'd like to understand what going wrong. sesion library is already loaded

$autoload['libraries'] = array('database','session');
#4

[eluser]adamp1[/eluser]
I take it your controller looks like this?
Code:
class Usn extends Controller {
   function __construct(){
      parent::Controller;
   }

   function index(){
      ...
   }
}

I'm afraid I have to go now, have a lecture I need to attend. If its not fixed later I will have a look then. Maybe someone else may take a look in the mean time.
#5

[eluser]zwippie[/eluser]
What is the output of:

var_dump($this->session->userdata(’auth’));

when you place this line of code in the constructor?


It could be that the session variable auth is not stored as an integer but as a string.
#6

[eluser]harmstra[/eluser]
sigh.....

forgot the parent::Controller();

now things do work!




Theme © iAndrew 2016 - Forum software by © MyBB