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


Messages In This Thread
Portable functions in OOP? - by El Forum - 12-21-2007, 04:16 PM
Portable functions in OOP? - by El Forum - 12-21-2007, 05:52 PM
Portable functions in OOP? - by El Forum - 12-21-2007, 05:54 PM
Portable functions in OOP? - by El Forum - 12-21-2007, 06:02 PM
Portable functions in OOP? - by El Forum - 12-21-2007, 06:05 PM
Portable functions in OOP? - by El Forum - 12-21-2007, 06:10 PM
Portable functions in OOP? - by El Forum - 12-21-2007, 06:13 PM
Portable functions in OOP? - by El Forum - 12-21-2007, 11:53 PM
Portable functions in OOP? - by El Forum - 12-22-2007, 12:42 AM
Portable functions in OOP? - by El Forum - 12-22-2007, 12:43 AM
Portable functions in OOP? - by El Forum - 12-22-2007, 04:41 PM
Portable functions in OOP? - by El Forum - 12-22-2007, 05:34 PM
Portable functions in OOP? - by El Forum - 12-22-2007, 10:04 PM
Portable functions in OOP? - by El Forum - 12-22-2007, 11:44 PM
Portable functions in OOP? - by El Forum - 12-23-2007, 12:27 AM
Portable functions in OOP? - by El Forum - 12-23-2007, 12:30 AM
Portable functions in OOP? - by El Forum - 12-23-2007, 12:33 AM



Theme © iAndrew 2016 - Forum software by © MyBB