CodeIgniter Forums
Undefined index: userid & Fatal error: ob_start - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Undefined index: userid & Fatal error: ob_start (/showthread.php?tid=17038)

Pages: 1 2


Undefined index: userid & Fatal error: ob_start - El Forum - 03-24-2009

[eluser]langithitam[/eluser]
Hy bro, i got this error when i tried to login

Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: controllers/welcome.php

Line Number: 129

Quote:Fatal error: ob_start() [ref.outcontrol]: Cannot use output buffering in output buffering display handlers in D:\htdocs\shopping\system\libraries\Exceptions.php on line 160

and this is the code

Code:
function verify(){
    if ($this->input->post('username')){
        $u = $this->input->post('username');
        $pw = $this->input->post('password');
        $this->MAdmins->verifyUser($u,$pw);
        if ($_SESSION['userid'] > 0){ [i][b]<-- this is line 129[/b][/i]
            redirect('admin/dashboard','refresh');
        }
    }
    $data['main'] = 'login';
    $data['title'] = "Claudia's Kids | Admin Login";
    $data['navlist'] = $this->MCats->getCategoriesNav();
    $this->load->vars($data);
    $this->load->view('template');  
  }

Any help will be really appreciated.. Undecided


Undefined index: userid & Fatal error: ob_start - El Forum - 03-24-2009

[eluser]pistolPete[/eluser]
You have to check if userid is set before you compare its value:
Code:
if( isset($_SESSION['userid']) )
{
   if ($_SESSION['userid'] > 0)
   {
       ...
   }
}

Btw: You could also use CI's session class: http://ellislab.com/codeigniter/user-guide/libraries/sessions.html


Undefined index: userid & Fatal error: ob_start - El Forum - 03-24-2009

[eluser]langithitam[/eluser]
[quote author="pistolPete" date="1237903941"]You have to check if userid is set before you compare its value:
Code:
if( isset($_SESSION['userid']) )
{
   if ($_SESSION['userid'] > 0)
   {
       ...
   }
}

Btw: You could also use CI's session class: http://ellislab.com/codeigniter/user-guide/libraries/sessions.html[/quote]
i'll try it first..


Undefined index: userid & Fatal error: ob_start - El Forum - 03-24-2009

[eluser]Stelian Mocanita[/eluser]
Code:
function verify(){
    if ($this->input->post('username')){
        /** Request comes from users, we should xss filter this (more at http://ellislab.com/codeigniter/user-guide/libraries/input.html **/
        $u  = $this->input->post('username', TRUE);
        $pw = $this->input->post('password', TRUE);
        
        /** Returning a result here would be faster than writing to session and reading the session since your function returns something anyway **/
        $this->MAdmins->verifyUser($u,$pw);

        /** Better yet use difference in both value and type than just is higher **/
        if ($this->session->userdata('userid') !== 0){
            redirect('admin/dashboard','refresh');
        }
    }
    $data['main'] = 'login';
    $data['title'] = "Claudia's Kids | Admin Login";
    $data['navlist'] = $this->MCats->getCategoriesNav();
    $this->load->vars($data);
    $this->load->view('template');  
  }

Also in your MAdmin::verifyUser() where you set the session, your CI rewritten session would be:

Code:
$userdata['userID']    =      $userID;
$userdata['loginTime'] =      time();

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

Make sure to have the session class loaded. Should you use PHP's session_autostart you could add Session in autoload(config/autoload.php in the libraries array) or a simple controller call

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



Undefined index: userid & Fatal error: ob_start - El Forum - 03-24-2009

[eluser]langithitam[/eluser]
@pistolPete, i tried it there is no error. But now, i can't login with ID.. :question:

Quote:Sorry, your username or password is incorrect!



Undefined index: userid & Fatal error: ob_start - El Forum - 03-24-2009

[eluser]langithitam[/eluser]
@Stelian Mocanita. I tried it, but now i just showing me another error message,

Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: admin/dashboard.php

Line Number: 8

Quote:A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at D:\htdocs\shopping\system\libraries\Exceptions.php:164)

Filename: helpers/url_helper.php

Line Number: 485

have any clue..?


Undefined index: userid & Fatal error: ob_start - El Forum - 03-24-2009

[eluser]Stelian Mocanita[/eluser]
having that the if clauses conditions work left to right you could use them in the same condition like

Code:
if( isset($_SESSION['userid']) and $_SESSION['userid'] > 0)
{
    /** Work your magic here **/
}

The code snippet pistolPete wrote is correct, you have a session miss behavior somewhere else.

LE: Can you please post the MAdmin::Verify() code here?


Undefined index: userid & Fatal error: ob_start - El Forum - 03-24-2009

[eluser]langithitam[/eluser]
Yes Stelian, here it is :

Code:
class MAdmins extends Model{

    function MAdmins(){
        parent::Model();
    }


    function verifyUser($u,$pw){
        $this->db->select('id,username');
        $this->db->where('username',db_clean($u,16));
        $this->db->where('password', db_clean(dohash($pw),16));
        $this->db->where('status', 'active');
        $this->db->limit(1);
        $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!');
        }        
    }



Undefined index: userid & Fatal error: ob_start - El Forum - 03-25-2009

[eluser]Stelian Mocanita[/eluser]
Here is the CI-Session based code:

Code:
class MAdmins extends Model{

    function MAdmins(){
        parent::Model();
    }


    function verifyUser($u,$pw){
        $this->db->select('id,username');
        $this->db->where('username',db_clean($u,16));
        $this->db->where('password', db_clean(dohash($pw),16));
        $this->db->where('status', 'active');
        $this->db->limit(1);
        $Q = $this->db->get('admins');

        if ($Q->num_rows() > 0){
            $row = $Q->row_array();
            $userdata['userid']   = $row['id'];
            $userdata['username'] = $row['username'];
            $this->session->set_userdata($userdata);
            return TRUE;
        }else{
            $this->session->set_flashdata('error', 'Sorry, your username or password is incorrect!');
            return FALSE;
        }        
    }

and the other snipper:

Code:
function verify(){
    if ($this->input->post('username')){
        /** Request comes from users, we should xss filter this (more at http://ellislab.com/codeigniter/user-guide/libraries/input.html **/
        $u  = $this->input->post('username', TRUE);
        $pw = $this->input->post('password', TRUE);
        
        /** Returning a result here would be faster than writing to session and reading the session since your function returns something anyway **/
        $isLogged = $this->MAdmins->verifyUser($u,$pw);

        /** Better yet use difference in both value and type than just is higher **/
        if ($isLogged === TRUE){
            redirect('admin/dashboard','refresh');
        }
    }
    $data['main'] = 'login';
    $data['title'] = "Claudia's Kids | Admin Login";
    $data['navlist'] = $this->MCats->getCategoriesNav();
    $this->load->vars($data);
    $this->load->view('template');  
  }

This should work for you, if not please dump the database results so we can see what happens.


Undefined index: userid & Fatal error: ob_start - El Forum - 03-27-2009

[eluser]langithitam[/eluser]
Sory, still can't do Stelian. It keep telling me that i got a wrong username or password. Sory stelian i'm newbie, how can i dump the database results? Is it just a common dump using phpmyadmin? thanks.