CodeIgniter Forums

Full Version: MY_controller problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Near[/eluser]
Hi! I'm creating a program which allows user to register and log in to a specific page. This is my problem:

1) validates user if registered or not
2) determine if the user is currently logged in

i managed to finish the first issue.
but the second issue. I created a my_controller which extends the ci_controller.
then i declare a global variable called $is_login.. I can set its value to true on one page. But when i load another page. The value of the variable $is_login was set to false.. Am i doing it wrong?do i have to store the variable to session instead of using my_controller? Thanks!

El Forum

[eluser]Near[/eluser]
here is my_controller
Code:
class base_c extends ci_controller{
protected $is_login;
function __construct(){
parent::__construct();
$this->load->library('auth');
$this->is_login = (bool) $this->auth->is_login();
}
}

welcome controller

Code:
class welcome extends base_c{

function __contruct(){
parent::__construct();
var_dump($this->is_login);
}

}
class auth
Code:
function is_login(){
return true;
}

El Forum

[eluser]marcogmonteiro[/eluser]
Code:
class base_c extends ci_controller{
      protected $is_login;
      
      function __construct(){
          parent::__construct();
          $this->load->library('auth');
          echo ($this->auth->is_login() = FALSE) ? 'you're are not logged in' :  'you are now logged in';
     }
}

perhaps something like this?

El Forum

[eluser]CroNiX[/eluser]
If you are going to extend a MY_Controller you will need an autoloader for it to find the class. It also doesn't look like you are extending things properly and/or perhaps don't have the extended base classes in the appropriate directory (/application/core)
http://philsturgeon.co.uk/blog/2010/02/C...ing-it-DRY

El Forum

[eluser]Near[/eluser]
Yes im able to set the $this->is_login to true if already logged in and false if not. The only problem that i encountered is that, for example im on the home page. The value of $is_login is true but when i click for example activities the value of $is_login is now FALSE..

El Forum

[eluser]weboap[/eluser]
can you start by capping the
Code:
class Base_c extends CI_Controller{

then

Code:
//then echo the value of

$this->auth->is_login();

and post back the values in the 2 pages

El Forum

[eluser]Near[/eluser]
Hi! Sorry for late reply. Everything is working fine. The capitalizations,declarations of variable. My_controller is located at applications/core... The only problem that i encounter is that when i request aq new page using redirect or other methods the controller variables are being set to FALSE. Do i have to store a session variable telling the user is logged in nd unset it after he logged out?

El Forum

[eluser]weboap[/eluser]
using session is the most common.

http://ellislab.com/codeigniter/user-gui...sions.html
Code:
$newdata = array(
                   'username'  => 'johndoe',
                   'email'     => '[email protected]',
                   'logged_in' => TRUE
               );

$this->session->set_userdata($newdata);


then
Code:
$logged_in = $this->session->userdata('logged_in');
if(!$logged_in){
.......
}
else
{
.......
}