I am trying to prevent duplicate entries with codeigniter, but it doesn't work anymore.
The data still insert to database despite the same name.
This is the controller code
public function add(){
$title['title']='Add New';
$data['data']='Insert New Group';
$data['exist']= 'This Group already exists';
$this->load->view('add_pbk_g', $data);
}
public function save(){
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('namegroup', 'Name', 'trim|required|is_unique[pbk_groups.Name]');
if($this->form_validation->run()==false){
$this->add_pbk_g($Name);
} else{
$this->db->insert('pbk_groups',array('Name'=> $this->input->post('namegroup')));
$this->load->model('mpbk_grup');
if($this->mpbk_grup->check_grup_exist('$Name')){
$title['title']='Add New';
$data['data']='Insert New Group';
$data['exist']= 'This Group already exists';
$this->load->view('layout/header', $title);
$this->load->view('add_pbk_g');
$this->load->view('layout/footer');
} else{
$this->mpbk_grup->add;
redirect('cpbk_grup/index');
}
}
}
this is the Model..
function add($data){
return $this->db->create('pbk_groups', $data);
}
function check_grup_exist($namegroup){
$this->db->where('Name', $namegroup);
$this->db->from('pbk_groups');
$query = $this->db->get();
if($query->num_rows() >0){
return $query->result();
} else {
return $query->result();
//return False;
}
}
and this is the view
<form method="post" class="form-horizontal" action="<?php echo site_url('cpbk_grup/save');?>">
<div class="box-body">
<?php echo validation_errors(); ?>
<div class="form-group">
<label class="col-sm-2 control-label">Nama Group</label>
<div class="col-sm-10">
<input type="text" name="namegroup" required="" class="form-control" placeholder="Nama Group">
</div>
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
<button type="submit" name="submit" value="submit" class="btn btn-info pull-right">Save</button>
Kembali
</div>
<!-- /.box-footer -->
</form>
public function save(){
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('namegroup', 'Name', 'trim|required|is_unique[pbk_groups.Name]');
if($this->form_validation->run()==false){
$this->add_pbk_g($Name);
} else{
$data = array ('Name' => $this->input->post('namegroup'));
$this->mpbk_grup->check_grup_exist($data);
}
}
Where does $Name on this line come from?
if($this->mpbk_grup->check_grup_exist('$Name')){
Also it should not have quotes around it, should be...
if($this->mpbk_grup->check_grup_exist($Name)){
Try doing this inside save function
Controller Code:
$data = array('Name' => $this->input->post('namegroup'));
$this->Model_Name->check_grup_exist($data);
My Model
Model_Name
$this->main_table = 'pbk_groups';
public function check_grup_exist($data)
{
$query = $this->db->get_where($this->main_table, array('Name' => $data['Name']));
if ($query->num_rows() == 0)
{
$this->db->insert($this->main_table, $data);
return $this->db->insert_id();
}
else
{
return false;
}
Checking duplication data before inserting the data. Let me know if any query occurs.
Related
I'm trying to create some admin login. I think my code is correct because when I input wrong email and password the error message appears on login page. However, when I input correct data of my DB, it also shows error message.
Please help me. I appreciate any answer.
my controller (Admin.php):
public function index()
{
$this->admindashboard();
}
public function admindashboard()
{
$data = array();
/* $data['main_content'] = $this->load->view('admin_main', '', TRUE); */
$this->load->view('admin/admin_main', $data);
}
public function admin_regst()
{
$data = array();
$data['main_content'] = $this->load->view('admin-regist', '', TRUE);
$this->load->view('admin/admin_main', $data);
}
My other controller (Login_admin.php):
public function index()
{
$this->load->view('login');
}
public function adminchecklogin()
{
$data = array();
$adminemail = $this->input->post('admin_email', TRUE);
$adminpassword = $this->input->post('admin_psw', TRUE);
$this->load->model('M_login_admin');
$admindetails = $this->M_login_admin->admin_login_check($adminemail);
if (password_verify($adminpassword, $admindetails->admin_psw)) {
if ($admindetails->admin_status == 1) {
$session_data['adminid'] = $admindetails->admin_id;
$session_data['adminemail'] = $admindetails->admin_email;
$session_data['adminusername'] = $admindetails->admin_username;
$session_data['adminstatus'] = $admindetails->admin_status;
$this->session->set_userdata($session_data);
redirect('Admin');
} else {
$data['error_msg'] = "User ini tidak aktif....!!!";
redirect('login', $data);
}
} else {
redirect('error-login', $data);
}
}
public function login_error()
{
$data['error_msg'] = "Email atau Password Anda Salah....!!!";
$this->load->view('login', $data);
}
my model (M_login_admin.php):
public function admin_login_check($adminemail)
{
$admin_details = $this->db->select('*')
->from('admin')
->where('admin_email', $adminemail)
->get()
->row();
return $admin_details;
}
my view(login.php):
<body>
<section class="hero is-fullheight">
<div class="hero-body container has-text-centered">
<div class="login">
<img src="https://logoipsum.com/logo/logo-1.svg" width="325px" />
<p>
<?php
if (isset($success_msg)) {
echo $success_msg;
}
?>
</p>
<p>
<?php
if (isset($error_msg)) {
echo $error_msg;
}
?>
</p>
<form action="<?= base_url(); ?>Login_admin/adminchecklogin" method="POST">
<div class="box">
<div class="field">
<div class="control">
<input class="input is-medium is-rounded" type="email" placeholder="hello#example.com" autocomplete="username" name="admin_email" required />
</div>
</div>
<div class="field">
<div class="control">
<input class="input is-medium is-rounded" type="password" placeholder="**********" autocomplete="current-password" name="admin_psw" required />
</div>
</div>
<div class="field has-text-left ml-3 mt-5">
<label class="checkbox">
<input type="checkbox">
Remember me
</label>
</div>
</div>
<button class="button is-block is-fullwidth is-primary is-medium is-rounded" type="submit">
Login
</button>
</form>
<br>
</div>
</div>
</section>
My route
DB Column
DB Data
Interface
Your database has the admin_psw in plain text that would explain why the password_verify() is failing.
In order to use password_verify(), you have to have hashed the password using password_hash() This would normally be done when the user first registers or any time the user changes their password
Check the documentation for password_hash() in the PHP manual
how to alert a message if the bill no is not in the database
<form action="<?=site_url('TipUp_Loan/Bill_Delete')?>" class="form-inline" method="POST">
<div class="modal-body">
<div class="form-group">
<label>Bill No: </label>
<input type="text" id="bill" class="form-control" name="Search1" autofocus>
</div>
</div>
<div class="modal-footer text-center">
<button type="submit" id="Delete" onclick="return confirm('Are You sure want to Delete')" class="btn btn-primary" >Delete<i class="icon-bin position-left"></i></button>
</div>
</form>
This is view code....
public function Bill_Delete(){
$session_data = $this->session->userdata('logged_in');
$data['username'] = $session_data['username'];
$Search = $this->input->post('Search1');
$this->User_model->Bill_Delete($Search);
}
This is a controlller code...
public function Bill_Delete($Search)
{
$this->db->where('billno', $Search);
$this->db->delete('salesitem');
$this->db->where('no', $Search);
$this->db->delete('salesbill');
//echo "Successfully delted";
$this->session->set_flashdata('Add', 'You Deleted The Bill No Successfully');
redirect("Inventory/Bill_Entry","refresh");
}
This is model code...
my problem is how to find that bill no is present in the database, if it not in the database it should alert the message...
You need to use $this->db->affected_rows(); function which return true on bill is deleted else false.
Hope this will help.
//In controller function
public function Bill_Delete(){
$this->form_validation->set_rules('billno','billno','exist[salesitem.billno]');
if ($this->form_validation->run() == FALSE)
{
return $this->set_response( array(), validation_errors(), 'Bill No not exits' );
}
else
{
$session_data = $this->session->userdata('logged_in');
$data['username'] = $session_data['username'];
$Search = $this->input->post('Search1');
$this->User_model->Bill_Delete($Search);
}
// In MY_Form_validation library
function exist($str, $value){
list($table, $column) = explode('.', $value, 2);
$query = $this->CI->db->query("SELECT COUNT(*) AS count FROM $table WHERE $column = $str'");
$row = $query->row();
return ($row->count > 0) ? FALSE : TRUE;
}
I'm having difficulties in understanding what's going on here.
I've made a login page, setting it to go to admin page. Without any user data yet, just to check if it's working. And It doesn't go. I hade some problems loading the library form_validation. So I added the parent construct.
Controller
class Login Extends CI_Controller{
public function __construct()
{
parent::__construct();
$this->load->helper('url');
}
public function index() {
$this->load->view('login');
$this->load->helper('url');
}
public function login() {
$this->load->library('form_validation');
$this->load->helper('url');
$this->form_validation->set_rules('username','Username','trim|required');
$this->form_validation->set_rules('password','Password','trim|required|md5');
if($this->form_validation->run()==false){
$this->index();
}else{
$user_session=array(
'Username' => $this->input->post('username'),
'Password' => $this->input->post('password'),
'is_logged_in' => 1
);
$this->session->set_userdata($user_session);
redirect('login/admin');
}
}
public function admin() {
$this->load->view('admin');
}
}
My Login View
<section class="login_content">
<?php echo validation_errors(); ?>
<form action="<?php echo base_url().'login/login'; ?>" method="post">
<h1>Login no Sistema</h1>
<div>
<input type="text" name="username" class="form-control" placeholder="Username" required="" />
</div>
<div>
<input type="password" name="password" class="form-control" placeholder="Password" required="" />
</div>
<div>
<input type="submit" name="submit" value="Login" />
</div>
<div class="clearfix"></div>
<div class="separator">
<div class="clearfix"></div>
<br />
<div>
<h1> Sitio Monica e Marcia</h1>
<p>©2016 Todos os direitos reservados. Sitio Monica e Marcia.</p>
</div>
</div>
</form>
<!-- form -->
</section>
My Admin View
<div id="login" class="animate form">
<section class="login_content">
<h1>Bem vindo ao Admin</h1>
<?php
echo '<pre>';
print_r($this->session->all_userdata());
echo '<pre>';
?>
<!-- form -->
</section>
<!-- content -->
</div>
Here Is Your Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/* Description: Login controller class*/
class Login extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->library('session');
$this->load->model('login_model');
}
public function admin($msg = NULL){
// Load our view to be displayed
// to the user
$data['msg'] = $msg;
if($msg == NULL)
{
$this->load->view('login');
}
else
{
//print_r($data);
//die();
$this->load->view('login',$data);
}
}
public function process(){
// Load the 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
$msg = 'Invalid username or password';
$this->admin($msg);
}else{
// If user did validate,
// Send them to members area
redirect('home/check_isvalidated');
}
}
public function doLogout(){
$this->session->sess_destroy();
redirect(base_url());
}
}
Here IS The Model You Can Use Validation instead of Security
public function validate(){
// grab user input
$username = $this->security->xss_clean($this->input->post('user_name'));
$password = $this->security->xss_clean(md5($this->input->post('password')));
// Prep the query
$this->db->where('user_name', $username);
$this->db->where('password', $password);
// Run the query
$query = $this->db->get('admin');
// 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(
'id' => $row->id,
'user_name' => $row->user_name,
'validated' => true
);
$this->session->set_userdata($data);
return true;
}
// If the previous process did not validate
// then return false.
else {
return false;
}
}
}
Try this
redirect(base_url('login/admin'));
I think, you should check your link and session. This is the code
login.php in controller
public function __construct() {
parent::__construct();
$this->load->helper('url');
}
public function index() {
//$this->load->view('login');
$this->load->view('login');
}
public function login() {
$this->load->library('session');
$this->load->library('form_validation');
$this->load->helper('url');
$this->form_validation->set_rules('username','Username','trim|required');
$this->form_validation->set_rules('password','Password','trim|required|md5');
if($this->form_validation->run()==false){
$this->index();
}else{
$user_session=array(
'Username' => $this->input->post('username'),
'Password' => $this->input->post('password'),
'is_logged_in' => 1
);
$this->session->set_userdata('userlogin',$user_session);
$this->admin();
}
}
public function admin() {
$this->load->view('admin');
}
login.php in view
<form class="form-horizontal" role="form" action="<?php echo 'http://localhost/answers/index.php/login/login'; ?>" method="post">
<div class="form-group">
<label class="control-label col-sm-2" for="username">Username:</label>
<div class="col-sm-10">
<input type="username" class="form-control" id="username" name="username" placeholder="Enter username">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="pwd">Password:</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="password" name="password" placeholder="Enter password">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<div class="checkbox">
<label><input type="checkbox"> Remember me</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">Login</button>
</div>
</div>
</form>
And admin.php in views
<div class="container" height=100%>
<h2>Welcome to the system,
<?php
$this->load->library('session');
$login_session = $this->session->userdata('userlogin');
//$username = $this->session->userdata('userlogin');
echo $login_session['Username'];
?>
</h2>
</div>
May you want to see the complete code like http://explicitphp.blogspot.co.id/2016/03/Codeigniter-Login-System-Tutorial-Without-Database-Connection.html
Hope answer your question. Have fun guys
first, i don't know if the Login.php file or the class Login extends CI_Controller is a reserved name, try changing it.
then you have a public function called login(), this can have problems with the constructor, because in php5 or above you call the constructor like the class name and you maybe override the constructor
I want to show an updated profile page of users on my site. The update on database is actually working but i want my userprofile page to show the updated datas once it clicked the submit button. What will I add here D:
MemberLoginController.php (controller)
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class MemberLoginController extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('MemberLoginModel');
}
public function home(){
$this->load->view('pages/index');
}
public function userprofile(){
$this->load->view('member/userprofile');
}
public function useredit(){
$this->load->view('member/useredit');
}
public function memberlogin(){
$this->form_validation->set_error_delimiters('<p class=error>','</p>');
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|callback_validate_credentials');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
if($this->form_validation->run()){
// Perform Actions after getting valid form inputs
$data = array(
'email' => $this->input->post('email'),
'is_logged_in' => 1
);
$this->session->set_userdata($data);
redirect('index.php/MemberLoginController/members');
}else
$this->load->view('pages/index');
}
public function members(){
if($this->session->userdata('is_logged_in')){
$vis = "hidden";
$id = $this->session->userdata('id');
$this->load->model('MemberLoginModel');
$memberinfo['memberinfo']=$this->MemberLoginModel->getMember($id);
$this->load->view('member/userprofile',$memberinfo);
}else{
redirect('index.php/HomeController/home');
}
}
public function edit($id){
$data = array(
"action" => base_url('/index.php/MemberLoginController/update/'.$id),
"data" => $this->db->get_where('member',array('id'=>$id))
);
$this->load->view('member/useredit', $data);
}
public function update($id){
$data = array(
'memberfname'=> $this->input->post('memberfname'),
'memberlname'=> $this->input->post('memberlname'),
'email'=>$this->input->post('email'),
);
$this->db->update('member',$data,array('id'=> $id));
redirect('index.php/MemberLoginController/getMember/'.$id);
}
public function getMember(){
$this->load->model('MemberLoginModel');
$memberinfo['memberinfo']=$this->MemberLoginModel->getMember();
$this->load->view('member/userprofile',$memberinfo);
}
public function validate_credentials(){
$this->load->model('MemberLoginModel');
if($this->MemberLoginModel->login()){
echo ("<SCRIPT LANGUAGE = 'JavaScript'>
window.alert('Login Successfully')
window.location.href='userprofile'
</SCRIPT>");
exit();
return true;
}else{
echo ("<SCRIPT LANGUAGE = 'JavaScript'>
window.alert('Invalid username or password. Please click LOGIN again.')
window.location.href='home'
</SCRIPT>");
exit();
//$this->form_validation- >set_message('validate_credentials','Incorrect Email/Password');
return false;
}
}
}
?>
MemberLoginModel.php
<?php class MemberLoginModel extends CI_Model{
public function __construct()
{
parent::__construct();
}
function login()
{
$this->db->where('email',$this->input->post('email'));
$this->db->where('password', md5($this->input->post('password')));
$query = $this->db->get('member'); /*i added 'member' table on db new members*/
if($query->num_rows()>0)
{
foreach($query->result() as $rows)
{
//add all data to session
$newdata = array(
'id' => $rows->id,
'memberfname' => $rows->memberfname,
'memberlname' => $rows->memberlname,
'email' => $rows->email,
'logged_in' => TRUE
);
}
$this->session->set_userdata($newdata);
return true;
}else{
return false;
}
}
public function add_user()
{
$data = array(
'memberfname'=>$this->input->post('memberfname'),
'memberlname'=>$this->input->post('memberlname'),
'email'=>$this->input->post('email'),
'password'=>md5($this->input->post('password')),
);
$this->db->insert('member',$data);
}
public function getMember()
{
$query=$this->db->get('member');
return $query->result();
}
}
?>
Userprofile.php (view)
<body>
<div class="row rowpadding">
<div class="col-md-3 col-sm-3">
<div class="user-wrapper">
<div class="description">
<img height="200px" width="200px" src="<?php echo base_url();?>upload/no-avatar.jpg">
</div>
<br><br>
</div>
</div>
<div class="col-md-6 col-sm-6 user-wrapper">
<div class="description">
<br>
<h2 class="name">Hi, <?php echo $this->session->userdata('memberfname'); ?>!</h2>
<hr/>
<div class="colwrapper">
<div class="cont-5">
<div class="cont-6 name"><p class="para-2"><span class="font-3">First Name: <?php echo $this->session->userdata('memberfname'); ?>
</span></p></div>
</div><br>
<div class="cont-7">
<div class="cont-8 name"><p class="para-3"><span class="font-4">Last Name: <?php echo $this->session->userdata('memberlname'); ?></span></p></div>
</div><br>
<div class="cont-9">
<div class="cont-10"><p class="para-4"><span class="font-5">Email Address: <?php echo $this->session->userdata('email'); ?></span></p></div>
</div><br>
<div class="cont-11">
<div class="cont-12"><p class="para-5"><span class="font-6"></span></p></div>
</div><br>
</div>
<br><br>
</div>
</div>
<div class="col-md-3 col-sm-3 user-wrapper">
<div class="user-wrapper">
<br>
<div class="description">
<ul class="rightnavi"style="">
<li class="rightnavi">Add Profile Photo</li>
<li class="rightnavi">Update Your Profile</li>
</ul>
<hr/>
</div>
</div>
</div>
<!-- USER PROFILE ROW END-->
</div>
</body>
Useredit.php (View)
<form action="<?php echo $action;?>" method="POST" enctype="multipart/form-data">
<div class="container">
<div class="row rowpadding">
<div class="col-md-3 col-sm-3">
<div class="user-wrapper">
<div class="description">
<img height="200px" width="200px" src="<?php echo base_url();?>upload/no-avatar.jpg">
</div>
<br><br>
</div>
</div>
<div class="col-md-6 col-sm-6 user-wrapper">
<div class="description">
<br>
<h2 class="name">Hi, <?php echo $this->session->userdata('memberfname'); ?>!</h2>
<hr />
<p>
<div class="colwrapper">
<div class="cont-5">
<div class="cont-6 name"><p class="para-2"><span class="font-3">First Name:
<input type="text" name="memberfname" value="<?php echo $this->session->userdata('memberfname'); ?>" required />
</span></p></div>
</div><br>
<div class="cont-7">
<div class="cont-8 name"><p class="para-3"><span class="font-4">Last Name:
<input type="text" name="memberlname" value="<?php echo $this->session->userdata('memberlname'); ?>" required /></span></p></div>
</div><br>
<div class="cont-9">
<div class="cont-10"><p class="para-4"><span class="font-5">Email Address:
<input type="text" name="email" value="<?php echo $this->session->userdata('email'); ?>" required /></span></p></div>
</div><br>
<div class="cont-11">
<div>
<input type="hidden" name="hidden" value="<?php echo $this->session->userdata('id'); ?>"/>
<input type="submit" value="update">
</div>
</div>
<br><br>
</p>
</div>
</div>
</div>
</div>
</div>
</form>
You should combine form_validation object with your code.
public function update($id)
{
if ( (int)$id < 1)//$id is not an integer
{
redirect('memberlogincontroller/home', 'refresh');
}
else
{
$this->load->library('form_validation');//it's good to autoload it
$this->form_validation->set_rules('memberfname', 'First Name', 'trim|required');
$this->form_validation->set_rules('memberlname', 'Last Name', 'trim|required');
$this->form_validation->set_rules('email', 'Email', 'trim|required');
if ($this->validation_form->run() === FALSE)
{
$this->load->view('userprofile');
}
else
{
$data = array(
'memberfname' => $this->input->post('memberfname'),
'memberlname' => $this->input->post('memberlname'),
'email' => $this->input->post('email'),
);
$this->db->update('member',$data,array('id'=> $id));
redirect('memberlogincontroller/getMember/' . $id, 'refresh');
}
}
}
In model you are not passing id of specific member. It should be like this
public function getMember($id)
{
$this->db->where('id', $id);
$query = $this->db->get('member');
if ($query->num_rows() !== 1)
{
return FALSE;
}
return $query->row();//since you need just single member data
}
Also, in controller method code you have to pass $id to model:
public function getMember($id)
{
if (int($id) < 1)
{
redirect('memberlogincontroller/home', 'refresh');//no valid uri segment
}
else
{
$this->load->model('MemberLoginModel');
$memberinfo['memberinfo'] = $this->MemberLoginModel->getMember($id);
if ( ! $memberinfo['memberinfo'])//returned FALSE from model
{
redirect('memberlogincontroller/home', 'refresh');//no $id in DB
}
else
{
$this->load->view('member/userprofile',$memberinfo);
}
}
}
Also, you should pay attention to naming convention. You shouldn't close file with closing php tag. My proposal is to read docs carefully since you could avoid lot of annoying bugs that way.
Redirect should be
redirect('MemberLoginController/members');
redirect('Controller/Method');
If method is not added, So it will call the index() method by default
I'm using codeigniter for a login form validation. The code was previously working fine, but when I made some modifications, it just do not work now.
When I try to type in wrong or leave blank for password or username, the error message is shown as normal, but when I type in the correct username and password, $this->form_validation->run() just give me FALSE with an empty validation_errors() string.
I've tried to restore my old working controller php, but this error resists.
Full code related is available at
User Controller http://pastebin.com/nt2SDVnv
Login View http://pastebin.com/9hEc0EJB
User Model http://pastebin.com/p1z0zLM8
Only related code are pasted below:
Controller:
<?php
class User extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library('form_validation');
$this->load->model('user_model');
$this->load->helper('url');
$this->load->helper('form');
$this->load->helper('cookie');
}
function login(){
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|callback_username_check');
$this->form_validation->set_rules('password', 'Password', 'trim|required|md5|callback_password_check');
$this->_username = $this->input->post('username'); //用户名
$remember_me = $this->input->post('remember_me');
$json_string = $this->input->cookie('userinfo');
$userinfo_json = json_decode($json_string);
if(isset($userinfo_json->username)){
if ($this->username_check($userinfo_json->username)){
$this->user_model->login($userinfo);
redirect('admin/dashboard');
}
}
if ($this->form_validation->run() == FALSE){
$this->load->view('account/login');
} else {
$userinfo=$this->user_model->get_by_username($this->_username);
$this->user_model->login($userinfo);
if($remember_me=="on"){$this->user_model->write_session($userinfo);}
redirect('admin/dashboard');
}
}
function username_check($username){
if ($this->user_model->get_by_username($username)){
return TRUE;
}else{
$this->form_validation->set_message('username_check', 'User name not exist.');
return FALSE;
}
}
function password_check($password) {
$password = md5($password);
if ($this->user_model->password_check($this->_username, $password)){
return TRUE;
}else{
$this->form_validation->set_message('password_check', 'Incorrect username or paswsword.');
return FALSE;
}
}
}
View:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login - <?=$this->admin_model->get_title();?></title>
<?php $this->load->view('gy/head');?>
</head>
<body>
<?php $this->load->view('gy/header');?>
<div class="hero-unit header">
<div class="container">
<div style="text-align:center;">
<h1>Sign in</h1>
<p class="lead">Log into <?=$this->admin_model->get_title();?>.</p>
</div>
</div>
</div>
<div class="container">
<div class="span5 offset3">
<?php if(validation_errors() !== '' || #(!$err_message == '')){ ?>
<div class="alert alert-error fade in">
×
<strong>Error!</strong> <?=validation_errors('<span>','</span>');?> <?=#$err_message?>
</div>
<?php } ?>
<?php if(#$message!=''){ ?>
<div class="alert fade in alert-success">
×
<strong>Success!</strong> <?=$message?>
</div>
<?php } ?>
<?php echo form_open('login',array('class'=>'form-horizontal')); ?>
<div class="control-group">
<label class="control-label" for="username">User name</label>
<div class="controls">
<input type="text" id="username" name="username" placeholder="User name">
</div>
</div>
<div class="control-group">
<label class="control-label" for="password">Password</label>
<div class="controls">
<input type="password" id="password" name="password" placeholder="Password">
</div>
</div>
<label for="remember_me" class="checkbox">
<input type="checkbox" id="remember_me" name="remember_me"> Remember me (30 days)
</label>
<div style="text-align:center;">
<input type="submit" name="submit" value="Log in" class="btn btn-primary">
</div>
</Form>
</div>
</div>
<?php $this->load->view('gy/footer');?>
</body>
</html>
Model:
<?php
class user_model extends CI_Model {
public function __construct()
{
parent::__construct();
$this->load->database();
$this->load->library('session');
}
function login($userinfo)
{
$data = array('username'=>$userinfo->username,
'user_id'=>$userinfo->id,
'role'=>$userinfo->role,
'logged_in'=>TRUE);
$this->session->set_userdata($data);
}
function write_session($userinfo)
{
$user_json = json_encode($userinfo);
$cookie = array(
'name' => 'userinfo',
'value' => $user_json,
'expire' => '2592000',
'secure' => TRUE
);
$this->input->set_cookie($cookie);
}
function get_by_username($username)
{
$this->db->where('username', $username);
$query = $this->db->get('users');
if ($query->num_rows() == 1)
{
return $query->row();
}
else
{
return FALSE;
}
}
function password_check($username, $password)
{
if($user = $this->get_by_username($username))
{
return $user->password == $password ? TRUE : FALSE;
}
return FALSE;
}
}
I suspect the trim rule. It doesn't return a Boolean and I suppose you haven't set any error message for that rule. trim() should be run on the username and password, after the validation runs successful. If you just wanna check if the input has blank spaces You could add a rule to check with the strpos() but again in a custom callback. Just remember to use the Identical opperator '===' instead of the equal '==' .
Controller :
$this->form_validation->set_rules('username','Username','callback_space_check|required|xss_clean|callback_username_check');
function space_check($input){
if(strpos(' ',$input)===false){
return TRUE;
}else{
$this->form_validation->set_message('space_check', '%s contains space.');
return FALSE;
}
}