Welcome Guest, Not a member yet? Register   Sign In
Portable functions in OOP?
#1

[eluser]Ludwig Wendzich[/eluser]
Hey,

Not sure if the title is the best but I'm not sure how to sum up what I'm asking. Basically I'm new to Code Igniter (got it yesterday) and chose it because everyone's been telling me I need to start using a framework and since I knew PHP and I regularly use EE so I chose CI.

Anyway, I'm currently writing a user-auth system for a web app I want to write in CI and this OOP thing is hard to get my head around (trust me, I have read and watched a lot of videos and articles!)

So I'm getting this annoying error,
Quote:Fatal error: Call to a member function userdata() on a non-object in .../system/application/models/student/auth_model.php on line 37

Here's some code as I hear that helps Wink

auth_model.php
Code:
<?php

class Auth_model extends Model {

    function Auth_model() {
    
        parent::Model();
        
        
    }
    function login($email, $password)
    {
        $user =& get_instance();
        $user->load->library('validation');
        
        $user->validation->set_error_delimiters('<p class="error">', '</p>');
        
        $rules['email']     = 'trim|required|valid_email';
        $rules['password']    = 'trim|required|md5';
        
        $user->validation->set_rules($rules);
        
        $fields['email'] = 'Email';
        $fields['password'] = 'Password';
        
        $user->validation->set_fields($fields);
        
        if($user->validation->run() == FALSE):
            return $user->load->view('student/login');
        else:
            $email = $_POST['email'];
            $password = $_POST['password'];
            
            //CHECK IF ALREADY LOGGED IN
            
            if($user->session->userdata('email') == $email):
                //User is already logged in.
                $user->load->view('student/already_logged_in');;
            endif;
            
            //QUERY INFO ON EMAIL
            
            $user->db->where('email', $email);
            $query = $user->db->getwhere('users');
            $row = $query->row();            
            
            //CHECK IF VERIFIED
            
            if($row->verified=="no"):
                            
                return $user->load->view('student/unverified');
            
            else:
                        
            //CHECK EMAIL AND PASS
                        

                if($password == $row->password):
                
                    $user->load->library('Auth');
                    $user->auth->set_login($row);
                    
                    
                    redirect('student', 'location');
                   else:
                    return $user->load->view('student/failed_login');
                endif;
                
            endif;
        endif;
    }
    
    
    function email_check($email)
    {
        $this->db->where('email', $email);
        $query = $this->db->getwhere('users');
        
        if ($query->num_rows() > 0):
            //username already exists
            $this->validation->set_message('email_check', 'The %s is already in use.');
            
            return false;
        else:
            return true;
        endif;

    }

}
?&gt;

login() in student.php
Code:
function login() {
        
        $this->load->model('student/auth_model');
        $auth = new Auth_model;
            
            if(isset($_POST['submit'])){
                $email         = $_POST['email'];
                $password    = $_POST['password'];
            }
            
        $auth->auth_model->login($email,$password);
        
    }

I think that's what you need...I auto-load 'database','session' and 'auth'.

Auth.php
Code:
&lt;?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class Auth extends Controller {

    function logged_in($cookie_logged_in)
    {
        if($cookie_logged_in==true):
            //User is already logged in.
            return true;
        else:
            return false;
        endif;
    
    }
    
    function set_login($row)
    {
                    
                    //Destroy old session
                    $this->session->sess_destroy();
                    
                    //Create a fresh, brand new session
                    $this->session->sess_create();
                    
                    //Remove the password field
                    unset($row->password);
                    
                    //Set session data
                    $this->session->set_userdata($email);
                    
                    //Set logged_in to true
                    $this->session->set_userdata(array('logged_in' => true));
                    
                    return true;
    }
}

?&gt;
Hope this is all you need, please help me get m head around this!

Thanks!


EDIT: I've based some code on Simplelogin
#2

[eluser]tonanbarbarian[/eluser]
the main problem i can see is that no where in your code are you loading the session library
is it being autoloaded?
#3

[eluser]Ludwig Wendzich[/eluser]
Sorry, yea between the code snippets I said I load database, session and my own, auth, automagically Smile
#4

[eluser]tonanbarbarian[/eluser]
ok so what is the student.php file?
is it a controller?

reguardless though the way you are loading the model is wrong
Code:
function login() {
        
        $this->load->model('student/auth_model');
            
            if(isset($_POST['submit'])){
                $email         = $_POST['email'];
                $password    = $_POST['password'];
            }
            
        $this->auth_model->login($email,$password);
        
    }

loading it using
Code:
$auth = new Auth_model;
will stop all of the correct inheritance i think
#5

[eluser]Ludwig Wendzich[/eluser]
Quote:Fatal error: Class 'Auth_model' not found in /Applications/MAMP/htdocs/nceatracker/system/application/controllers/student.php on line 76

Then I get that...sorry if I'm an idiot with this...here's my code:

Code:
function login() {
        
        $auth = new Auth_model;
            
            if(isset($_POST['submit'])){
                $email         = $_POST['email'];
                $password    = $_POST['password'];
            }
            
        $auth->auth_model->login($email,$password);
        
    }

And yea, student.php is a controller...
#6

[eluser]tonanbarbarian[/eluser]
umm no use this
Code:
function login() {
        
        $this->load->model('student/auth_model');
            
            if(isset($_POST['submit'])){
                $email         = $_POST['email'];
                $password    = $_POST['password'];
            }
            
        $this->auth_model->login($email,$password);
        
    }
#7

[eluser]Ludwig Wendzich[/eluser]
I had that to begin with Sad

I did try it again, coping your code exactly, and got:

Quote:Fatal error: Call to a member function login() on a non-object in /Applications/MAMP/htdocs/nceatracker/system/application/controllers/student.php on line 83
#8

[eluser]tonanbarbarian[/eluser]
since the problem is now in the student controller can you show all of the code for it

do you have a constructor in the student controller? I noticed you do not have one in the auth controller either

make sure it has something like this
Code:
&lt;?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class Student extends Controller {

  function Student() {
    parent::Controller();
  } // Student()

    function login() {
        
        $this->load->model('student/auth_model');
            
            if(isset($_POST['submit'])){
                $email         = $_POST['email'];
                $password    = $_POST['password'];
            }
            
        $this->auth_model->login($email,$password);
        
    }    // login()

} // class Student

If you do not have the constructor then you will not be able to load things correctly in the model
#9

[eluser]Ludwig Wendzich[/eluser]
This is my code for student.php
Code:
&lt;?php

class Student extends Controller {

    function Student()
    {
        parent::Controller();
        $this->load->helper('form');
        $this->load->library('auth');

        $this->output->enable_profiler(TRUE);    
    }
    
    function index()
    {
        if($this->auth->logged_in($this->session->userdata('logged_in'))):
            $this->load->view('student/already_logged_in');
        else:
            $this->auth->login();
        endif;
    }
    
    
    function register()
    {        
        if($this->auth->logged_in($this->session->userdata('logged_in'))):
            $this->load->view('student/already_logged_in');
        else:
            
            $this->load->library('validation');
                
            $this->validation->set_error_delimiters('<p class="error">', '</p>');
            
            $rules['password']        = "trim|required|min_length[6]|matches[v_password]|md5";
            $rules['v_password']    = "trim|required";
            $rules['email']            = "trim|required|valid_email|callback_email_check";
            $rules['level']            = "trim|required|numeric";
            $rules['firstname']        = "trim|required|alpha";
            $rules['lastname']        = "trim|alpha";
    
            $this->validation->set_rules($rules);
    
            $fields['email']        = 'Email';
            $fields['password']        = 'Password';
            $fields['v_password']    = 'Verify Password';
            $fields['level']        = 'NCEA Level';
            $fields['firstname']    = 'First Name';
            $fields['lastname']        = 'Last Name';
        
            $this->validation->set_fields($fields);
            
            
            if ($this->validation->run() == FALSE)
            {
                $this->load->view('student/register');
            }
            else
            {
                $data = array(
                                   'email'         => $_POST['email'] ,
                                   'firstname'     => $_POST['firstname'] ,
                                   'lastname'     => $_POST['lastname'] ,
                                   'password'    => $_POST['password'] ,
                                   'level'        => $_POST['level'],
                                   'ver_num'    => md5($_POST['email'])
                             );
    
                $this->db->insert('users', $data);
                $this->load->view('student/registered');
            }
        endif;
    }
    
function login() {
        
        $this->load->model('student/auth_model');
            
            if(isset($_POST['submit'])){
                $email         = $_POST['email'];
                $password    = $_POST['password'];
            }
            
        $this->auth_model->login($email,$password);
        
    }
    
    /*function login()
    {
        
        $this->load->library('validation');
        
        $this->validation->set_error_delimiters('<p class="error">', '</p>');
        
        $rules['email']     = 'trim|required|valid_email';
        $rules['password']    = 'trim|required|md5';
        
        $this->validation->set_rules($rules);
        
        $fields['email'] = 'Email';
        $fields['password'] = 'Password';
        
        $this->validation->set_fields($fields);
        
        if($this->validation->run() == FALSE):
            $this->load->view('student/login');
        else:
            $email = $_POST['email'];
            $password = $_POST['password'];
            
            //CHECK IF ALREADY LOGGED IN
            
            if($this->session->userdata('email') == $email):
                //User is already logged in.
                $this->load->view('student/already_logged_in');
                return false;
            endif;
            
            //QUERY INFO ON EMAIL
            
            $this->db->where('email', $email);
            $query = $this->db->getwhere('users');
            $row = $query->row();            
            
            //CHECK IF VERIFIED
            
            if($row->verified=="no"):
                            
                $this->load->view('student/unverified');
            
            else:
                        
            //CHECK EMAIL AND PASS
                        

                if($password == $row->password):
                
                    //Destroy old session
                    $this->session->sess_destroy();
                    
                    //Create a fresh, brand new session
                    $this->session->sess_create();
                    
                    //Remove the password field
                    unset($row->password);
                    
                    //Set session data
                    $this->session->set_userdata($row);
                    
                    //Set logged_in to true
                    $this->session->set_userdata(array('logged_in' => true));
                    redirect('student', 'location');
                   else:
                    $this->load->view('student/failed_login');
                endif;
                
            endif;
        endif;
    }*/
    
    
    function verify()
    {
        $data['vn'] = $this->uri->segment(3);
        
        $this->db->where('ver_num', $data['vn']);
        $update = array(
                        'verified'    =>'yes'
                        );
        $this->db->update('users', $update);
        
        $this->load->view('student/verify');
    
    }
    
    
    function logout()
    {
        $this->session->sess_destroy();
        redirect('student/login', 'location');
    }
}
?&gt;
#10

[eluser]Ludwig Wendzich[/eluser]
O woops, didn't realise I had the commented out login()....anyway,

Wanted to say, but couldnt fit it the above post, that I'm not sure which is the controller, if it's the functon with the same name as the class I'll add one to Auth...


I'll do that anyway to see if it works...




Theme © iAndrew 2016 - Forum software by © MyBB