CodeIgniter Forums
problems with $_SESSION - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: problems with $_SESSION (/thread-26249.html)



problems with $_SESSION - El Forum - 01-09-2010

[eluser]_SC_[/eluser]
Hi all,
I have some troubles with $_SESSION.
In my controller I have :
Code:
$this-> MAdmins-> verifyUser($u,$pw);
if ($_SESSION['userid'] > 0){
redirect('admin/dashboard','refresh');
}
In my module I have:
Code:
function verifyUser($u,$pw){
        ...
        $Q = $this-> db-> get('admins');
        if ($Q-> num_rows() > 0){
            $row = $Q-> row_array();
            $_SESSION['userid'] = $row['id'];
            $_SESSION['username'] = $row['username'];
        }else{
$this-> session-> set_flashdata('error', 'Sorry, your username or password is incorrect!');
        }
    }
But if I I insert the incorrect data, when i return at the controller in the row :
Quote:if ($_SESSION['userid'] > 0) ...
I get an error: Message: Undefined index: userid
How I can fix this?

Another problem is : why i dont get the messange set in flashdata?
how set_flashdata works? When you print the message? Inside the module or into the controller when you use the $_SESSION object?


problems with $_SESSION - El Forum - 01-09-2010

[eluser]gigas10[/eluser]
Did you declare session_start() in the constructor of your controller class just after the parent::Controller() line?
Code:
function __construct(){
    parent::Controller();
    session_start();
    $this->load->model('my_model');
}



problems with $_SESSION - El Forum - 01-09-2010

[eluser]_SC_[/eluser]
yes:

function Welcome()
{
parent::Controller();
session_start();
}


problems with $_SESSION - El Forum - 01-09-2010

[eluser]gigas10[/eluser]
Oh I see the problem, at least how to get rid of the error message:
Code:
if (isset($_SESSION['userid']) && $_SESSION[‘userid’] > 0){
    //do whatever
}

Also to display flash data you do it in the view.

Code:
echo $this->session->flashdata('error');



problems with $_SESSION - El Forum - 01-09-2010

[eluser]_SC_[/eluser]
oh thank you very much!