Welcome Guest, Not a member yet? Register   Sign In
Login function Problem
#1

(This post was last modified: 11-06-2017, 04:23 AM by paulboss. Edit Reason: -> Further info() )

Just a starter in CI. I already have my registration function working but I'm having a difficulty in in my login function.  I played with using CI Forms then switched to typical HTML input forms. Below are my MVC codes. 

EDIT: Result will only show white screen.
Server: XAMPP
CI v3.1.6


Controller:

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

class Users extends CI_Controller {

    function __construct()
    { //load the needed libraries and classes
        parent::__construct();
        $this->load->library('form_validation');
        $this->load->model('User');
    }
    //function for account checking
    public function account()
    {
        //check if the user is login or not
        $data = array();
        if($this->session->userdata('isUserLoggedIn'))
        {//if true the user will be redirected to userview page
            $data['User'] = $this->User->getRows(array('id' =>$this->session->userdata(userId)));
            $this->load->view('userview',$data);
        }
        else
        {    //user will be redirected to registration form
            redirect('register');
        }
    }//function to do login
    public function login()
    {
        $data = array();
        if($this->session->userdata('success_msg'))
        {
            $data['success_msg'] = $this->session->userdata('success_msg');
            $this->session->unset_userdata('success_msg');
        }
        if($this->input->post('loginSubmit')) //submit name for the button
        {    //validate the submitted input
            $this->form_validation->set_rules('username','username','required|max_length[20]|min_length[5]');
            $this->form_validation->set_rules('password','password','required|max_length[20]|min_length[8]');
            if($this->form_validation->run())
            {
                $con['returntype'] = 'single';
                $con['conditions'] = array (
                        'username' => $this->input->post('username'),
                        'password' => $this->input->post('password')
                );
                $checkLogin = $this->User->getRows($con);
                if($checkLogin)
                {
                    $this->session->set_userdata('isUserLoggedIn,TRUE');
                    $this->session->set_userdata('userID',$checkLogin['username']);
                    $this->session->set_flashdata('Logged_success','Welcome!');
                        redirect(base_url(),'userview');
                }
                else
                {
                    $this->session->set_flashdata('LogF','Login failed! Check your credentials.');
                        redirect(base_url(),'home');
                }
            }
        }
    }
 
Model:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User extends CI_Model {
    function __construct()
    {
        //$this->userTbl = 'users';
        $this->load->database();
    }

    function getRows($params = array())
    {
        $this->db->select('*');
        $this->db->from('users');

            //set conditions

        if(array_key_exists("conditions", $params))
        {
            foreach ($params ['conditions'] as $key => $value)
            {
                $this->db->where($key,$value);
            }
        }
        if(array_key_exists("username", $params))
        {
            $this->db->where('username',$params['username']);
            $query = $this->db->get();
            $result = $query->row_array();
        }
      
     else
        {
            //set start and limit
            if(array_key_exists("start",$params) && array_key_exists("limit",$params))
            {
                $this->db->limit($params['limit'],$params['start']);
            }
            elseif(!array_key_exists("start", $params) && array_key_exists("limit", $params))
            {
                $this->db->db($params['limit']);
            }

            $query = $this->db->get();
            if(array_key_exists("returnType", $params) && $params['returnType'] == 'count')
            {
                $result = $query->num_rows();
            }
            elseif(array_key_exists("returnType", $params) && $params['returnType'] == 'single')
            {
                $result = ($query->num_rows() > 0 ) ?$query->row_array(): FALSE;
            }
            else
            {
                $result = ($query->num_rows() > 0) ?$query->row_array(): FALSE;
            }
            return $result;
        }
    }

Views:
Code:
<?php if($this->session->flashdata('username1')) : ?>
    <p class="alert alert-dismissable alert-danger"><?php  echo $this->session->flashdata('username1');   ?></p>
<?php endif; ?>

    <form action="<?php base_url(); ?>Users/login" method="post">
        <div class="form-group has-feedback">
        <p><input class="form-control" type="text" name="username" placeholder="ID Number"></p>
        <p><input class="form-control" type="password" name="password" placeholder="!@#$%^&*"></p>
        <p><input class="btn btn-primary" type="submit" name="loginSubmit" value="Submit">
        </div>
        <a href="<?php echo base_url('register');?>"> No account yet? | Register</a></p>
    </form>
Reply
#2

Hello,

I suggest you to use a dedicated library like : http://benedmunds.com/ion_auth/
Reply
#3

(11-06-2017, 03:59 AM)bvrignaud Wrote: Hello,

I suggest you to use a dedicated library like : http://benedmunds.com/ion_auth/

Hi! Thank you for the suggestion will discover this idea. But if you have feedbacks on the codes above it will be appreciated.
Reply
#4

(11-06-2017, 04:28 AM)paulboss Wrote:
(11-06-2017, 03:59 AM)bvrignaud Wrote: Hello,

I suggest you to use a dedicated library like : http://benedmunds.com/ion_auth/

Hi! Thank you for the suggestion will discover this idea. But if you have feedbacks on the codes above it will be appreciated.

Your code stores passwords in plain-text and that's an absolute no-go. Using a vetted library will solve such problems for you automatically, and if you're in it for the learning - you can study the library's code.
Reply
#5

(11-06-2017, 05:08 AM)Narf Wrote:
(11-06-2017, 04:28 AM)paulboss Wrote:
(11-06-2017, 03:59 AM)bvrignaud Wrote: Hello,

I suggest you to use a dedicated library like : http://benedmunds.com/ion_auth/

Hi! Thank you for the suggestion will discover this idea. But if you have feedbacks on the codes above it will be appreciated.

Your code stores passwords in plain-text and that's an absolute no-go. Using a vetted library will solve such problems for you automatically, and if you're in it for the learning - you can study the library's code.

Yep! I intentionally did that just to observe the behavior once I can make it run I will just change to more secure methods. Thank you for the heads up.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB