User Login

#11
[eluser]GamingFusion[/eluser]
Ok i added this code to my .htaccess file

Code:
php_value display_errors 1
php_value error_reporting 2039

and that gave me an error about get_cookie()

so i forgot to load the cookie helper.

now i get this error

Quote:Fatal error: Cannot use object of type CI_DB_mysql_result as array in /Users/chadgregory/Sites/ci/system/application/models/database.php on line 129

heres my code for the database.php file(model) with a star on line 129

Code:
function checkLogin()
    {
    
        $this->load->helper('cookie');
            
        $u1 = get_cookie('username');
        $p1 = get_cookie('password');

        $logged = $this->db->get_where('users', array ('username' => $u1, 'password' => $p1));

        //Rename some of the $logged['variables'] with easier names.
*        $uId = $logged['id'];
        $uName = $logged['username'];
        
        if ($uName) {
            return TRUE;
        }else{
            return FALSE;
        }
    }

basically i want it to assign the id and username of the user who is logged in into the two variables

#12
[eluser]clip[/eluser]
Please read the user guide on how to work with query results

Code:
$logged = $this->db->get_where('users', array ('username' => $u1, 'password' => $p1));

if($logged->num_rows() > 0)
{
     $row = $query->row();

     $uId = $row->id;
     $uName = $row->username;
}

#13
[eluser]GamingFusion[/eluser]
ok that worked thanks man, the code i ahd there before was from my user system i made in php a few months ago. But now i was wandering how to return the variable $uId and $uName to the page that i execute the function on so i can do something like this:

Code:
<?="Your Id is: ", $uId?>
<?="Your Id is: ", $uName?>

#14
[eluser]clip[/eluser]
You can either add them to a session or change your return from your model
Code:
//change
return TRUE;
//to
$data['username'] = $uName;
$data['user_id'] = $uId;
return $data;

#15
[eluser]GamingFusion[/eluser]
did work man here my code

Code:
function checkLogin()
    {
    
        $this->load->helper('cookie');
            
        $u1 = get_cookie('username');
        $p1 = get_cookie('password');

        $logged = $this->db->get_where('users', array ('username' => $u1, 'password' => $p1));

        if($logged->num_rows() > 0)
        {
             $row = $query->row();

             $uId = $row->id;
             $uName = $row->username;
            
            $data['userId'] = $uId;
            $data['userName'] = $uName;
            
            return $data;
        }
    }

#16
[eluser]clip[/eluser]
[quote author="GamingFusion" date="1257318289"]did work man here my code

Code:
function checkLogin()
    {
    
        $this->load->helper('cookie');
            
        $u1 = get_cookie('username');
        $p1 = get_cookie('password');

        $logged = $this->db->get_where('users', array ('username' => $u1, 'password' => $p1));

        if($logged->num_rows() > 0)
        {
             $row = $query->row();

             $uId = $row->id;
             $uName = $row->username;
            
            $data['userId'] = $uId;
            $data['userName'] = $uName;
            
            return $data;
        }
    }
[/quote]
To get access to your returned data you need to do in your controller
Code:
$userdata = $this->model_name->checkLogin();
if($userdata)
{
    echo $userdata['userId'];
    //or assign it to another variable to use later in your view
  
}

Also to make things cleaner instead of returning just the $data array you could
Code:
return $row;
//instead of
return $data

//then you have access to the entire query result
//from your controller you would then do
$userdata = $this->model_name->checkLogin();
if($userdata)
{
    echo $userdata->table_field_name;
    //or assign it to another variable to use later in your view
  
}

#17
[eluser]GamingFusion[/eluser]
ok thanks man that makes sense

#18
[eluser]GamingFusion[/eluser]
still nothing

heres my function that im use to get the variables to display

Code:
function index()
    {
        //then you have access to the entire query result
        //from your controller you would then do
        $userdata = $this->database->checkLogin();
        if($userdata)
        {
            $data['uId'] = $userdata->id;
            //or assign it to another variable to use later in your view
  
        }
        
        $data['title'] = 'Theater 311';
        $data['heading'] = 'Welcome to Theater 311';
        
        $this->load->view('main', $data);
    }

The title and heading variables work but not uId

#19
[eluser]jedd[/eluser]
Quote:The title and heading variables work but not uId

This might be how to suck eggs stuff, but when you say 'not uId' it's unclear what you've done.

Obviously the first thing would be an echo 'hi!' in your if {} block, just to prove out you're getting past the conditional. Or even a var_dump.

I'd like to assume you've already done this, as it's pretty basic PHP debugging stuff - but if you had, then you'd have solved your problem already I think.

#20
[eluser]GamingFusion[/eluser]
theres something wrong with the login script.

The Login script isn't checking anything

Code:
function login()
    {
        $this->load->helper('cookie');    
    
        $username = $this->input->post('username');
        $password =  $this->input->post('password');
        
        $checkUser = $this->db->get_where('users', array('username' => $username, 'password' => $password));
        
        if ($checkUser == FALSE) {
            
            echo 'Password is incorrect.';
            
        }else{
            
                $cookie = array(
                   'username'   => $username,
                   'passdword'  => $password,
               );

                set_cookie($cookie);
                
                redirect('theater', 'refresh');
        }        
    }


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.