Session library in codeigniter

I want to check if the user is loggedin in codeigniter and set this into config/routing.php the nedded controller as default and redirect all request depending on what level of access has the user.

I make in config/routing.php until now something like this:

if ($_SESSION['loggedin'] == TRUE){
    $route['default_controller'] = $_SESSION['class'];
    $route['(.*)']               = $_SESSION['class']."/$1";
} else {
    $route['default_controller'] = 'first';
}

I tryed with $this->session->userdata() but i recive error because i think the config/autoload.php or session is loaded after config/routing.php.

Any ideeas ?

Answers


Load session in codeigniter like this

$this->load->library('session');

But its better to load in autoload.php.

its not $_SESSIONS its $_SESSION

if ($_SESSION['loggedin'] == TRUE){
    $route['default_controller'] = $_SESSION['class'];
    $route['(.*)']               = $_SESSION['class']."/$1";
} else {
    $route['default_controller'] = 'first';
}

in codeigniter use like hits,

if($this->session->userdata("loggedin") == TRUE)
{
    $route['default_controller'] = $this->session->userdata("class");
    $route['(.*)'] = $this->session->userdata("class")."/$1";
} else {
    $route['default_controller'] = 'first';
}

Ok i have made to work without errors. I have commented two lines from system/libraries/Session/Session.php:

session_start();  (Line:140)
ini_set('session.use_trans_sid', 0);   (Line:313) 

And added to appliction/config/routes.php the session_start(). It works witout error.

I hope helps anyone.


Need Your Help

How do I create crash log when the Android process crashes

android logging crash acra

My Java code will invoke native code. When the native code crashes, I want to write the crash log to my own file. How can I achieve this? I have used ACRA, but it won't work for native code.

Make and build utilities on CentOS/RHEL?

build centos utilities rhel

I've been unsuccessfully searching for a way to install make utility on my CentOS 5.2. I've looked through some RPM repositories and online, with no avail. Installing gcc, gcc-c++ didn't help! Pack...