3

I'm just new using codeIgniter and I wanted to create a simple login with sign up and session.

Here's my code...

<?php if(!defined('BASEPATH')) exit('No direct script access allowed');

class LoginVerify extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('userModel', '', TRUE);
    }

    function index() {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('username', 'Username', 'trim|required');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|callback_this_database');
        if($this->form_validation->run()==FALSE) {
            $this->load->view('login_view');
        }
        else {
            redirect('home', 'refresh');
        }
    }

    function this_database($password) {
        $username=$this->input->post('username');
        $result=$this->user->login($username, $password);
        if($result) {
            $sess_array=array();
            foreach($result as $row) {
                $sess_array=array('id'=>$row->id, 'username'=>$row->username);
                $this->session->set_userdata(logged_in, $sess_array);   
            }
            return TRUE;
        }
        else {
            $this->form_validation->set_message(this_database, 'Invalid username or password');
            return FALSE;
        }
    }
}
?>

I don't know why I'm still receiving error when trying to run this program.

1
  • This would be nice if you will indicate the error that you encountered Commented Aug 5, 2014 at 10:47

4 Answers 4

2

i think because of this

    else {
        redirect('home', 'refresh');
        //means you are going to controller named home, but your controller name is LoginVerify
    }

guess it will solve your problem

Sign up to request clarification or add additional context in comments.

3 Comments

In ci redirect('home') means home controller it will redirect home controller home/index
there are so many errors in code like $this->session->set_userdata(logged_in, $sess_array);
I have code in Home. That's part of my View in codeigniter
1

You are calling db queries on callback try your method part in after validation success also create a alias for usermodel. also check you have already included session library. Also you left syntax errors so try my full code:-

<?php if(!defined('BASEPATH')) exit('No direct script access allowed');

class LoginVerify extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('userModel', 'user');
    }

    function index() {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('username', 'Username', 'trim|required');
        $this->form_validation->set_rules('password', 'Password', 'trim|required');
        if($this->form_validation->run()==FALSE) {
            $this->load->view('login_view');
        }
        else {
            $username=$this->input->post('username');
            $password=$this->input->post('password');
            $result=$this->user->login($username, $password);
            if($result) {
                $sess_array=array();
                foreach($result as $row) {
                    $sess_array=array('id'=>$row->id, 'username'=>$row->username);
                    $this->session->set_userdata('logged_in', $sess_array);   
                }
                redirect('home', 'refresh');
            }
            else {
                $this->form_validation->set_message('this_database', 'Invalid username or password');
                redirect('home', 'refresh');
            }

        }
    }
}
?>

8 Comments

yes my library included here. Error move to this line $this->session->set_userdata(logged_in, $sess_array);
check updated it will be $this->session->set_userdata('logged_in', $sess_array);
wow. thanks. I got it. I can now proceed creating account. Thanks a lot. :)
Oh I see, here wait. Thanks again!
@user3709308 if you newbie you also need this suggestion. not the same meaning i have 2 yr exp on stack so i know this thing
|
1
$this->form_validation->set_rules('username', 'Username', 'trim|required');

$this->form_validation->set_rules('password', 'Password', 'trim|required');

Comments

-2

try to change  

$this->load->model('userModel', '', TRUE);

to  

$this->load->model('userModel', 'user');

and better remove php close tag at the end of the script

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.