0

hi all, please kindly give me advice and help me on my project this is just a piece of it. thanks and God Bless.

i got this database error

**A PHP Error was encountered

Severity: Notice

Message: Undefined index: username

Filename: controllers/login.php

Line Number: 20
A PHP Error was encountered

Severity: Notice

Message: Undefined index: password

Filename: controllers/login.php

Line Number: 21**

and this would be my controller

login.php --controller

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

class login extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {

        $this->load->helper(array('form', 'url'));
        $this->load->view('login_view');

    }

    public function regprocess()
    {
        $username=$_POST['username'];
        $password=$_POST['password'];

        $this->user->insert($username,$password);

        redirect('login');


    }
}
?>
3
Contributors
6
Replies
9
Views
4 Years
Discussion Span
Last Post by LastMitch
0

@nicoalmighty

i got this database error

May I ask what PHP mainframe are you using?

It's bit hard to understand without the whole code. So base on the code and your error

it means that you can't fetch the username & password from your DB.

If you can provide the DB side it will make more sense!

1

LastMitch I think it's CodeIgniter framework :)

@nico
Change lines 20 and 21 with:

 $username = $this->input->post('username');
 $password = this->input->post('password');

so each field will be sanitized and if it is empty or wrong will return FALSE: http://codeigniter.com/user_guide/libraries/input.html

bye!

Edited by cereal

Votes + Comments
I knew that but I wasn't sure! ;)
0

it's code ignter frame work cereal is right :)

okay i'll try it now. thanks :)

0

it's code ignter frame work cereal is right :)

okay i'll try it now. thanks :)

0

it worked thanks :)

oh i have another problem when i login it doesn't direct me to the private page but only shows 'undefined' what's the problem here?

i'll let you see my whole code by the way i'm using codeigniter and xampp as mysql and database

i will gladly appreciate you help :D

this is the login_view

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 0.1 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Login</title>
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <?php echo link_tag('css/themes/default/jquery.mobile-1.1.1.css'); ?>
    <script src="<?php echo base_url(); ?>js/jquery.js"></script>
    <script src="<?php echo base_url(); ?>js/jquery.mobile-1.1.1.min.js"></script>
</head>

<body data-role="page">

    <div>
    <div data-role="header">
    <h1>Login</h1>
    </div>

    <div data-role="content">
    <?php echo validation_errors(); ?>
    <?php echo form_open('VerifyLogin'); ?>
    <?php echo 'Username: ' . form_input('username',''). '<br />'; ?>
    <?php echo 'Password: ' . form_password('password',''). '<br />'; ?>
    <!--<label for="username">Username:</label>
    <input type="text" id="username" name="username"/>
    <br/>
    <label for="password">Password:</label>
    <input type="password" id="password" name="password"/>
    <br/>-->
    <input data-role="button" data-inline="true"  type="submit" value="login"/>
    <?php echo form_close(); ?>
    </div>





        <div data-role="header">
        <h1>Register Here</h1>
        </div>

        <div data-role="content">
        <?php echo form_open('login/regprocess'); ?>
        <?php echo 'Username: ' . form_input('username',''). '<br />'; ?>
        <?php echo 'Password: ' . form_password('password',''). '<br />'; ?>
        <?php echo form_submit('submit','Submit'); ?>
        <?php echo form_reset('reset','Clear'); ?>

              <?php echo form_close(); ?>
        </div>
    </div>   


</body>
</html>

the login controller

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

class login extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {

        $this->load->helper(array('form', 'url'));
        $this->load->view('login_view');

    }

    public function regprocess()
    {
        $username = $this->input->post('username');
        $password = $this->input->post('password');

        $this->user->insert($username,$password);

        redirect('login');


    }
}
?>

then the verifylogin controller

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

class VerifyLogin extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('user','',TRUE);
    }

    public function index()
    {
        //this method will have the credentials validation
        $this->load->library('form_validation');

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

        if($this->form_validation->run() == FALSE)
        {
            //field validation failed. user redirected to login page
            $this->load->view('login_view');
        }
        else
        {
            //go to private area
            redirect('home','refresh');
        }
    }

    public function check_database($password)
    {
        //field validation succeeded. validate against database
        $username = $this->input->post('username');




        //query the database
        $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('check_database','Invalid username or password');
            return false;
        }
    }
}
?>

the home_view

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 0.1 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Private Area</title>
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <?php echo link_tag('css/themes/default/jquery.mobile-1.1.1.css'); ?>
    <script src="<?php echo base_url(); ?>js/jquery.js"></script>
    <script src="<?php echo base_url(); ?>js/jquery.mobile-1.1.1.min.js"></script>
</head>

<body data-role="page">

    <div data-role="header">
    <h1>Home</h1>
    </div>

    <h2>Welcome <?php echo $username; ?>!</h2>

    <a href="home/logout" data-role="button" data-inline="true" data-mini="true" data-theme="a">Logout</a>
</body>
</html>

the home controller

<?php if(!defined('BASEPATH')) exit('no direct script access allowed');
    session_start(); //we need to call PHP's session object to access it through CI

    class home extends CI_Controller
    {

        public function __construct()
        {
            parent::__construct();
        }

        public function index()
        {
            if($this->session->userdata('logged_in'))
            {
                $session_data = $this->session->userdata('logged_in');
                $data['username']= $session_data['username'];
                $this->load->view('home_view',$data);
            }
            else
            {
                //if no session , redirect to login page
                redirect('login','refresh');
            }
        }

        public function logout()
        {
            $this->session->unset_userdata('logged_in');
            session_destroy();
            redirect('home','refresh');
        }
    }
?>

and lastly the model

<?php 
Class User extends CI_Model
{

    public function insert($username, $password)
    {
        $entries = array(
            'id' => 0,
            'username' => $username,
            'password' => $password
            );

        $this->db->insert('users',$entries);



    }


    public function login($username,$password)
    {

        $this->db->select('username, password');
        $this->db->from('users');
        $this->db->where('username', $username);
        $this->db->where('password', $password);
        $this->db->limit(1);

        $query = $this->db->get();

        if($query->num_rows() == 1)
        {
            return $query->result();
        }
        else
        {
            return false;
        }
    }
}
?>
0

oh i have another problem when i login it doesn't direct me to the private page but only shows 'undefined' what's the problem here?

It means it's false, another words it's not reading the identify the username & password.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.