Welcome Guest, Not a member yet? Register   Sign In
having problems with session_start
#1

[eluser]Unknown[/eluser]
I am reading the CI book by Wrox and am going through the sample code. Everything was goiing fine until I was in Chpater 6, where I had to run through a native PHP session to construct an administrator login. Copied the code, tried it but I get an error indicating that the session variables I had assigned value with in my models does not exist. Here are the main points:

The Controller:
Code:
$user = $this->input->post('username');
$password = $this->input->post('password');
$this->MAdmins->verifyUser($user, $password);
if($_SESSION['userid'] > 0)
{
    redirect('admin/dashboard');
}

The Model:
Code:
//perform database query
$this->db->select('id','username');
$this->db->where('username', $user);
$this->db->where('password', $pass);
$this->db->where('status', 'active');
$this->db->limit(1);
$query = $this->db->get('administrator');

//perform result verification and assign to session variables
if($query->num_rows() > 0)
{
        $row = $q->row_array();
    $_SESSION['userid'] = $row['id'];
    $_SESSION['username'] = $row['username'];
}

The View:
Code:
<?php
echo form_open('administrator/login');
echo form_label('Username','username');
$udata = array(
    'name' => 'username',
    'id' => 'username',
    'size' => 40
);
echo form_input($udata) . '<br/>';

echo form_label('Password', 'password');
$pdata = array(
    'name' => 'password',
    'id' => 'password'
);
echo form_password($pdata) . '<br/>';
echo form_submit('submit', 'Submit');
?&gt;
&lt;/form&gt;


I am getting an error undefined index : userid or undefined index: username

I have started the session via:

Code:
function Administrator()
{
    parent::Controller();
    session_start();
}

What could I be doing wrong?
#2

[eluser]TheFuzzy0ne[/eluser]
Welcome to the CodeIgniter forums.

Your select query needs to look like this:

Code:
$this->db->select('id, username');

The method only takes two arguments. The first one is the string representing the fields you want to select, and the second is to prevent escaping, for times you you need to do something like this:
Code:
$this->db->select('COUNT(*)', FALSE);
#3

[eluser]Unknown[/eluser]
whoa! I knew that mechanism yet why haven't I noticed that! Will try rty now...hope this works now. I was already banging my head against the wall thinking that I may have put session_start() in the wrong place.




Theme © iAndrew 2016 - Forum software by © MyBB