Login3 - El Forum - 11-01-2013
[eluser]davy_yg[/eluser]
I create login with CI:
controllers/admin/clogin.php
Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/* Author: Jorge Torres
* Description: Login controller class
*/
class Clogin extends CI_Controller{
function __construct(){
parent::__construct();
}
public function index(){
// Load our view to be displayed
// to the user
$this->data['assets'] = array('logincss' => base_url().'assets/css/login.css',
'logo2' => base_url().'assets/images/logo2.png'
);
$this->load->view('admin/login', $this->data['assets']);
}
public function process(){
// Load the model
$this->load->model('Login_model');
// Validate the user can login
$result = $this->Login_model->validate();
// Now we verify the result
if(! $result){
// If user did not validate, then show them login page again
$this->index();
}else{
// If user did validate,
// Send them to members area
redirect('admin');
}
}
}
?>
models/login_model.php
Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/* Author: Jorge Torres
* Description: Login model class
*/
class Login_model extends CI_Model{
function __construct(){
parent::__construct();
}
public function validate(){
// grab user input
$ids_user = $this->input->post('ids_user', TRUE);
// Prep the query
$this->db->get_where('ids_user', $username);
// Run the query
$query = $this->db->get('ids_user');
// Let's check if there are any results
if($query->num_rows() == 1)
{
// If there is a user, then create session data
$row = $query->row();
$data = array(
'username' => $row->username,
'password' => $row->password,
'validated' => true
);
$this->session->set_userdata($data);
return true;
}
// If the previous process did not validate
// then return false.
return false;
}
}
?>
views/admin/login.php
Code: <html>
<head>
<link href="<?php echo $logincss; ?>" rel="stylesheet" type="text/css" media="screen">
</head>
<body>
<div id="logoadmin"></div>
<div id="loginbox">
<img src="<?php echo $logo2; ?>" width="150">
<br><br>
<div id="username">
<br><br>
<div id="position">
<?php
echo form_open('admin/clogin/process');
echo form_label('Username: ', 'username');
$dataUsername = array(
'username1' => 'username1',
'id' => 'username1',
'value' => '',
'size' => '10',
'style' => 'width:43%'
);
echo form_input($dataUsername); ?><br>
<?php
echo form_label('Password: ', 'password');
$dataPassword = array(
'password' => 'password',
'id' => 'password',
'value' => '',
'size' => '10',
'style' => 'width:43%'
);
echo form_password($dataPassword);
?><br>
</div>
<div id="submit">
<?php echo form_submit(array('value' => 'login', 'class' => 'button')); ?>
</div>
<?php form_close(); ?>
</div>
</div>
</body>
</html>
When I try to login it starts to bring me to this url:
http://localhost/IndonusaCI/index.php?admin/clogin/process (home page)
If type this url:
http://localhost/IndonusaCI/index.php/admin/clogin/process
This error appears:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Clogin::$db
Filename: core/Model.php
Line Number: 51
Fatal error: Call to a member function get_where() on a non-object in C:\xampp\htdocs\IndonusaCI\application\models\login_model.php on line 18
line 18: $this->db->get_where('ids_user', $username);
My questions are:
1. Why it shows error on line 18 ?
2. Why it bring me to the home page instead of the admin page after login ?
Login3 - El Forum - 11-07-2013
[eluser]DuyK-DCT[/eluser]
I guess that you've not read ci db active record document as well.
First thing. You must load database after call "db" from ci super object. Like this (It will solve your first error)
Code: $this->load->database();
$this->db->(..something you need to call from db object.)
And your problem in line 18 ? You call get_where as wrong with your intention.
I guess you want to run this query: SELECT * FROM `user` WHERE `username` = 'input-username';
So when you call get_where() function, it included full query string. Like this
Code: $this->load->database();
$this->db->get_where('user', array('username' => $username)); // 2nd parameter must be array
// It's equal to query
// SELECT * FROM `user` WHERE `username` = 'input-username'
$this->db->get_where('user', array('username' => $username, 'password' => $password)); // Your 2nd param can be have multi where statement
// It's equal to query
// SELECT * FROM `user` WHERE `username` = 'input-username' and `password` = 'input-password'
And your code above must be fix to:
Code: $this->load->database(); // You can load database as automaticaly with autoload.php in /config folder
$this->db->where('username', $username);
$query = $this->db->get('user');
Login3 - El Forum - 11-11-2013
[eluser]davy_yg[/eluser]
I replace the code:
Code: $this->db->get_where('ids_user', $username);
with
Code: $this->load->database(); // You can load database as automaticaly with autoload.php in /config folder
$this->db->where('ids_user', array('username' => $username));
$query = $this->db->get('ids_user');
and it still brings me to this url: http://localhost/IndonusaCI/index.php?verifylogin
after I login.
Login3 - El Forum - 11-12-2013
[eluser]DuyK-DCT[/eluser]
Sorry my friend, this is my mistake (When comment for help you im so sleepy)
Its must be:
Code: $this->load->database(); // You can load database as automaticaly with autoload.php in /config folder
$this->db->where('username', $username); // Or you can take multi where with $this->db->where(array('username' => $username, 'password' => $password));
$query = $this->db->get('ids_user');
|