I'm trying to make a login system and I'm getting this database error
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SET last_activity = 1467333267, user_data = 'a:3:{s:9:\"user_data\";s:0:\"\"' at line 1
UPDATE SET last_activity = 1467333267, user_data = 'a:3:{s:9:\"user_data\";s:0:\"\";s:5:\"email\";s:16:\"ocyl28#yahoo.com\";s:12:\"is_logged_in\";i:1;}' WHERE session_id = '570d7ba3d7f11a9a35264ed276368480'
Filename: C:/xampp/htdocs/CodeIgniter/system/database/DB_driver.php
Line Number: 691
Here is my controller code:
class Template extends CI_Controller {
public function index()
{
$this->login();
}
public function login()
{
$this->load->view('Login Template.php');
}
public function admin()
{
if ($this->session->userdata('is_logged_in')) {
$this->load->view('Template 3.php');
}
else {
echo 'cannot login';
}
}
public function login_validation()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('email' , 'Email', 'required|trim|callback_validate_credentials');
$this->form_validation->set_rules('password' , 'Password', 'required|md5|trim');
if ($this->form_validation->run()) {
$data = array (
'email' => $this->input->post('email'),
'is_logged_in' => 1
);
$this->session->set_userdata($data);
redirect('template/admin');
}
else
{
echo 'fail';
}
}
public function validate_credentials()
{
$this->load->model('users');
if ($this->users->can_log_in()) {
return true;
}
else
{
$this->form_validation->set_message('validate_credentials', 'fail to login');
return false;
}
}
public function about()
{
$this->load->view('About this site.php');
}
public function logout()
{
$this->session->sess_destroy();
redirect('template/login');
}
And here is my model code:
class Users extends CI_Model
{
public function can_log_in()
{
$this->db->where('email', $this->input->post('email'));
$this->db->where('password', md5($this->input->post('password')));
$query = $this->db->get('registered_users');
if ($query->num_rows() == 1) {
return true;
}
else
{
return false;
}
}
Table name is missing from your SQL statement.
Related
I've created a project in Codeigniter. My problem is when I log in, auth controller shows the value that is set in session $this->session->userdata("logged_in") but it is not redirecting to dashboard.
I also changed the PHP version on the live server from PHP 7.1 to PHP 5.6 but it's still not working. Session works perfectly on local server with xampp but not working on live server
Auth_model
public function Authentification() {
$notif = array();
$email = $this->input->post('email',TRUE);
$password = Utils::hash('sha1', $this->input->post('password'), AUTH_SALT);
$this->db->select('*');
$this->db->from('users');
$this->db->where('email', $email);
$this->db->where('password', $password);
$this->db->limit(1);
$query = $this->db->get();
if ($query->num_rows() == 1) {
$row = $query->row();
if ($row->is_active != 1) {
$notif['message'] = 'Your account is disabled !';
$notif['type'] = 'warning';
} else {
$sess_data = array(
'users_id' => $row->users_id,
'first_name' => $row->first_name,
'email' => $row->email
);
$this->session->set_userdata('logged_in', $sess_data);
}
} else {
$notif['message'] = 'Username or password incorrect !';
$notif['type'] = 'danger';
}
return $notif;
}
Auth controller
class Auth extends CI_Controller {
function __construct() {
parent::__construct();
Utils::no_cache();
if ($this->session->userdata('logged_in')) {
redirect(base_url('dashboard'));
exit;
}
}
public function index() {
redirect(base_url('home'));
}
public function login() {
$data['title'] = 'Login';
$this->load->model('auth_model');
if (count($_POST)) {
$this->load->helper('security');
$this->form_validation->set_rules('email', 'Email address', 'trim|required|valid_email|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');
if ($this->form_validation->run() == false) {
// $data['notif']['message'] = validation_errors();
// $data['notif']['type'] = 'danger';
$status = validation_errors();
if ( $this->input->is_ajax_request() ) {
echo json_encode($status);
exit;
}
}
else {
$data['notif'] = $this->auth_model->Authentification();
// it show the result here but not redirect to dashboard
// print_r($this->session->userdata("logged_in"));
// die("auth/login");
}
}
if ($this->session->userdata('logged_in')) {
redirect(base_url('dashboard'));
exit;
}
/*
* Load view
*/
$this->load->view('includes/header', $data);
$this->load->view('home/index');
$this->load->view('includes/footer');
}
dashboard
class Dashboard extends CI_Controller {
var $session_user;
function __construct() {
parent::__construct();
$this->load->model('auth_model');
$this->load->helper('tool_helper');
Utils::no_cache();
if (!$this->session->userdata('logged_in')) {
redirect(base_url('home'));
exit;
}
$this->session_user = $this->session->userdata('logged_in');
}
/*
*
*/
public function index() {
$data['title'] = 'Dashboard';
$data['session_user'] = $this->session_user;
// print_r($this->session->userdata("logged_in")); //its show empty
$data['items'] = $this->auth_model->get_all_products();
$this->load->view('includes/header', $data);
// $this->load->view('includes/navbar');
$this->load->view('includes/navbar_new');
$this->load->view('dashboard/index');
$this->load->view('includes/footer');
}
I don't know why session not set. I have been stuck in this for a week. Please help me out.
Try this.
Change $config['sess_save_path'] = sys_get_temp_dir(); to $config['sess_save_path'] = FCPATH . 'application/cache/sessions/'; in config.php
I keep on getting this error in my codeigniter micro app restful api. When I post an item only the first letter is get saved with status code 400 being displayed.
here is my model file:
class Cities_model extends CI_Model {
public function __construct()
{
parent::__construct();
}
public function save($city)
{
$this->db->set($this->setCity($city, null))->insert('cities');
if ($this->db->affected_rows() > 0) {
return $this->db->insert_id;
}
return null;
}
public function update($id, $city)
{
$this->db->set($this->setCity($city))->where('id')->update('cities');
if ($this->db->affected_rows() === 1) {
return true;
}
return false;
}
private function setCity($city)
{
return array(
'id' => $city['id'],
'name' => $city['name']
);
}
}
As you can see setCity function treat $city variable as array. So you need to pass array to setCity function.
class Cities_model extends CI_Model {
public function __construct()
{
parent::__construct();
}
public function save($city)
{
$this->db->insert('cities',$this->setCity(array('name'=>$city,'id'=> null)));
if ($this->db->affected_rows() > 0) {
return $this->db->insert_id();
}
return null;
}
public function update($id, $city)
{
$this->db->where('id',$id)->update('cities',$this->setCity(array('name'=>$city,'id'=> $id)));
if ($this->db->affected_rows() === 1) {
return true;
}
return false;
}
private function setCity($city)
{
return array(
'id' => $city['id'],
'name' => $city['name']
);
}
}
another thing is, Codeignitor having method insert_id() to know last insert id.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require APPPATH . '/libraries/REST_Controller.php';
class Cities extends REST_Controller{
public function __construct() {
parent::__construct();
$this->load->model('cities_model');
}
public function index_get(){
$cities=$this->cities_model->get();
if(!is_null($cities))
{
$this->response(array('response'=>$cities),200);
}
else
{
$this->response(array('error'=>'cities cannot be found...'),404);
}
}
public function find_get($id){
if(!$id)
{
$this->respose(null,400);
}
$cit=$this->cities_model->get($id);
if(!is_null($cit))
{
$this->response(array('response'=> $cit),200);
}
else{
$this->response(array('error'=> 'data could not be found...'),404);
}
}
public function index_post(){
// Use validation library, instead of checking just for value.
$this->load->library('form_validation');
$this->form_validation->set_rules('city','City','trim|required');
if($this->form_validation->run() == FALSE)
{
// send back list of validation errors.
$this->response($this->validation_errors(),REST_Controller::HTTP_BAD_REQUEST);
}
$id=$this->cities_model->save($this->post('city'));
if(!is_null($id))
{
$this->response(array('response'=> $id),REST_Controller::HTTP_OK);
}
else
{
$this->response(array('error'=> 'sorry, data could not be saved...'),REST_Controller::HTTP_BAD_REQUEST);
}
}
public function index_put(){
// for put you need to pass id as parameter
// Use validation library, instead of checking just for value.
$this->load->library('form_validation');
$this->form_validation->set_rules('id','ID','trim|required|integer');
$this->form_validation->set_rules('city','City','trim|required');
if($this->form_validation->run() == FALSE)
{
// send back list of validation errors.
$this->response($this->validation_errors(),REST_Controller::HTTP_BAD_REQUEST);
}
$update=$this->cities_model->update($this->post('id'),$this->post('city'));
if(!is_null($update))
{
$this->response(array('response' => 'content updated successfully'),REST_Controller::HTTP_OK);
}
else
{
$this->response(array('error'=> 'sorry, technical error occurred, please try again later...'), REST_Controller::HTTP_BAD_REQUEST);
}
}
public function index_delete($id){
if(!$id)
{
$this->response(null,400);
}
$del=$this->cities_model->delete($id);
if(!is_null($del))
{
$this->response(array('response'=> 'item successfully deleted'),200);
}
else{
$this->response(array('error'=> 'delete operations could not be done...'),400);
}
}
}
here is the model file:
<?php
class Cities_model extends CI_Model
{
public function __construct()
{
parent::__construct();
}
public function get($id=null)
{
if(!is_null($id))
{
$query=$this->db->select('*')->from('cities')->where('id',$id)->get();
if($query->num_rows()===1)
{
return $query->row_array();
}
return null;
}
$sql=$this->db->select('*')->from('cities')->get();
if($sql->num_rows()>0)
{
return $sql->result_array();
}
return null;
}
public function save($city)
{
$this->db->insert('cities', array('name'=>$city));
if($this->db->affected_rows()>0)
{
return $this->db->insert_id();
}
return null;
}
public function update($id, $city)
{
$this->db->where('id',$id)->update('cities',$this->setCity(array('name'=>$city,'id'=> $id)));
if ($this->db->affected_rows() === 1) {
return true;
}
return false;
}
private function setCity($city)
{
return array('id'=>$city['id'],
'name'=>$city['name']
);
}
public function delete($id)
{
$this->db->where('id',$id)->delete('cities');
if($this->db->affected_rows()===1)
{
return true;
}
return false;
}
}
I'm new in Codeigniter. I have a login system where user with status is 0 can't do login yet and where user with status 1 can login. I might be have a mistake in my code. So, i hope you can find where is my mistake and help me to make it right. here's my code.
My Controller
public function login() {
$this->form_validation->set_rules('no', 'No', 'required|min_length[10]|max_length[16]|integer');
$this->form_validation->set_rules('password', 'password', 'required|md5|xss_clean');
$this->form_validation->set_error_delimiters('<span class="error">', '</span>');
if($this->form_validation->run()== FALSE) {
$this->load->view('v_login');
}else{
$no = $this->input->post('no');
$password = $this->input->post('password');
$cek = $this->m_user->ambilPengguna($no, $password);
$status = $this->m_user->ambilStatus($no); //HERE'S THE PROBLEM
if($cek->num_rows()<> 0 && $status == '1') { //HERE'S TOO, IT WON'T CHECK THE STATUS.
$this->session->set_userdata('isLogin', TRUE);
$this->session->set_userdata('data_user',$cek->row());
redirect('c_belajar');
}else {
echo " <script>
alert('Login failed! call the administrator to activate your account');
history.go(-1);
</script>";
}
}
}
My Model
public function ambilPengguna($no, $password) {
$this->db->select('*');
$this->db->from('tb_user');
$this->db->where('no_id', $no);
$this->db->where('password', $password);
$query = $this->db->get();
return $query;
}
public function ambilStatus($no){
$this->db->select('status');
$this->db->from('tb_user');
$this->db->where('no_id', $no);
$query = $this->db->get();
return $query;
}
There's a mistake on controller. Please help me.
Alright! May I suggest a little code restructure
Controller
class YourController extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('login_model');
$this->load->library('form_validation');
}
public function login()
{
if($_POST)
{
$config=array(
array(
'field' => 'no',
'label' => 'Number',
'rules' => 'trim|required',
),
array(
'field' => 'password',
'label' => 'Password',
'rules' => 'trim|required',
)
);
$this->form_validation->set_rules($config);
if($this->form_validation->run()==false)
{
$data['errors']=validation_errors();
$this->load->view('login',$data);
}
else
{
$check=$this->login_model->checkUser($_POST); // you can use xss clean here filter post data
if(!$check)
{
$data['errors']='Invalid Password';
$this->load->view('login',$data);
}
elseif($check==1)
{
$data['errors']='Your account status is not active yet, Please contact Administrator';
$this->load->view('login',$data);
}
else
{
$this->session->set_uerdata($check);
redirect(base_url().'dashboard');
}
}
}
else
{
$this->load->view('login');
}
}
}
Model
class login_model extends CI_Model {
function __construct()
{
parent::__construct();
}
public function checkUser($data)
{
$st=$this->db->select('*')
->from('tbl_user')
->Where('no_id', $data['no'])
->where('password', $data['password'])
->get()->result_array();// you can use row()
if(count($st)>0)
{
if($st[0]['status']==0){
return 1;
}
else
{
return $st[0];
}
}
else
{
return false;
}
}
}
Try this, this working in my project..
Your Model
public function ambilStatus(){
$this->db->where('no_id', $this->input->post('your input name'));
$query = $this->db->get($this->db->dbprefix . 'tb_user');
$ret = $query->row();
return $ret->account_status;
}
And Your Controller
$status = $this->m_user->ambilStatus();
if($status && $cek->num_rows() == 1 ) {
I am trying to develop a login panel using codeigniter but I am unable to do so as I believe my concept is not so clear yet though or Am i doing something wrong please help me out with this concern
Controllers>admin.php
class admin extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('verify_user');
}
public function verify() {
$this->load->library('form_validation');
$username = $this->form_validation->set_rules('username', '', 'required|trim');
$password = $this->form_validation->set_rules('password', '', 'required|trim');
if($this->form_validation->run()) {
$this->verify_user->can_log_in();
redirect('admin/dashboard');
} else {
$this->load->view('admin/login');
}
}
public function dashboard() {
if($this->session->userdata('is_logged_id') == true) {
$this->load->view('admin/dashboard');
} else {
redirect('admin/login');
}
}
models>verify_users.php
class verify_user extends CI_Model {
public function __construct() {
parent::__construct();
}
public function can_log_in() {
$this->db->where('username', $this->input->post('username'));
$this->db->where('password', $this->input->post('password'));
$query = $this->db->get('users');
$query2 = $this->db->get_where('users', array(
'username' => $this->input->post('username')
));
if($query2->num_rows() == 1) {
$name = $query2->row()->first_name . " " . $query2->row()->last_name;
}
if($query->num_rows() == 1) {
$query = $this->db->get_where('users', array(
'username' => $this->input->post('username')
));
$data = array(
'username' => $this->input->post('username'),
'is_logged_in' => 1
);
$this->session->set_userdata('name', $name);
$this->session->set_userdata($data);
return true;
} else {
$data['message'] = 'Incorrect username/password';
$this->load->view('admin/login', $data);
}
}
}
The thing is happening when I login with correct username and password it redirects me back to login.php when I put the model script within the verify function it runs perfectly
Please help me out with this
This is the closest possible fix to your way of implementation.
You need to consider reading more about MVC.
Try replace your controller with this:
class admin extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('verify_user');
}
public function verify() {
$this->load->library('form_validation');
$username = $this->form_validation->set_rules('username', '', 'required|trim');
$password = $this->form_validation->set_rules('password', '', 'required|trim');
if($this->form_validation->run() && $this->verify_user->can_log_in()) {
redirect('admin/dashboard');
} else {
$this->load->view('admin/login');
}
}
public function dashboard() {
if($this->session->userdata('is_logged_in') == "1") {
$this->load->view('admin/dashboard');
} else {
redirect('admin/login');
}
}
}
And your model with this:
class verify_user extends CI_Model {
public function __construct() {
parent::__construct();
}
public function can_log_in() {
$this->db->where('username', $this->input->post('username'));
$this->db->where('password', $this->input->post('password'));
$query = $this->db->get('users');
$query2 = $this->db->get_where('users', array(
'username' => $this->input->post('username')
));
if($query2->num_rows() == 1) {
$name = $query2->row()->first_name . " " . $query2->row()->last_name;
}
if($query->num_rows() == 1) {
$query = $this->db->get_where('users', array(
'username' => $this->input->post('username')
));
$data = array(
'username' => $this->input->post('username'),
'is_logged_in' => "1"
);
$this->session->set_userdata('name', $name);
$this->session->set_userdata($data);
return true;
} else {
$data['message'] = 'Incorrect username/password';
return false;
}
}
}
Check this
class admin extends CI_Controller {
^// this should be Admin
IN model
else {
//$data['message'] = 'Incorrect username/password';
//$this->load->view('admin/login', $data);
//dont load view in model
return false;
}
In controller
if($this->form_validation->run()) {
$res = $this->verify_user->can_log_in();
if($res)
redirect('admin/dashboard');
else
redirect('admin/login');
} else {
$this->load->view('admin/login');
}
Fixing these 3 errors should help you.
I created a login system but every time I setup an if statement it loops back to the login page when I enter correct password. I need the index function in the controller, the list_employee function and View_employee function to redirect user to login page if they access it directly but if they enter correct password allow them to go to it.
user_authentication controller
<?php
session_start(); //we need to start session in order to access it through CI
Class User_Authentication extends CI_Controller {
public function __construct() {
parent::__construct();
// Load form helper library
$this->load->helper('form');
// Load form validation library
$this->load->library('form_validation');
// Load session library
$this->load->library('session');
// Load database
$this->load->model('login_database');
}
// Show login page
public function user_login_show() {
$this->load->view('login_form');
}
// Show registration page
public function user_registration_show() {
$this->load->view('registration_form');
}
// Validate and store registration data in database
public function new_user_registration() {
// Check validation for user input in SignUp form
$this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean');
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
$this->form_validation->set_rules('email_value', 'Email', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');
if ($this->form_validation->run() == FALSE) {
$this->load->view('registration_form');
} else {
$data = array(
'name' => $this->input->post('name'),
'user_name' => $this->input->post('username'),
'user_email' => $this->input->post('email_value'),
'user_password' => $this->input->post('password')
);
$result = $this->login_database->registration_insert($data) ;
if ($result == TRUE) {
$data['message_display'] = 'Registration Successfully !';
$this->load->view('login_form', $data);
} else {
$data['message_display'] = 'Username already exist!';
$this->load->view('registration_form', $data);
}
}
}
// Check for user login process
public function user_login_process() {
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');
if ($this->form_validation->run() == FALSE) {
$this->load->view('login_form');
} else {
$data = array(
'username' => $this->input->post('username'),
'password' => $this->input->post('password')
);
$result = $this->login_database->login($data);
if($result == TRUE){
$sess_array = array(
'username' => $this->input->post('username')
);
// Add user data in session
$this->session->set_userdata('logged_in', $sess_array);
$result = $this->login_database->read_user_information($sess_array);
if($result != false){
$data = array(
'name' =>$result[0]->name,
'username' =>$result[0]->user_name,
'email' =>$result[0]->user_email,
'password' =>$result[0]->user_password
);
redirect('employee');
}
}else{
$data = array(
'error_message' => 'Invalid Username or Password'
);
$this->load->view('login_form', $data);
}
}
}
// Logout from admin page
public function logout() {
// Removing session data
$sess_array = array(
'username' => ''
);
$this->session->unset_userdata('logged_in', $sess_array);
$data['message_display'] = 'Successfully Logout';
$this->load->view('login_form', $data);
}
}
?>
employee controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Employee extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('login/employee_model');
}
//Shows the dashboard
public function index()
{
$this->load->view('header');
$this->load->view('employee');
$this->load->view('login/footer');
}
//Insert the employee
public function insert_employee()
{
$data=array('name'=>$this->input->post('name'),
'LanId'=>$this->input->post('LanId'),
'reason'=>$this->input->post('reason'),
'PepNumber'=>$this->input->post('PepNumber'),
'Employee_Number'=>$this->input->post('Employee_Number'),
'department'=>$this->input->post('department'),
'status'=>1);
//print_r($data);
$result=$this->employee_model->insert_employee($data);
if($result==true)
{
$this->session->set_flashdata('msg',"Employee Records Added Successfully");
redirect('employee');
}
else
{
$this->session->set_flashdata('msg1',"Employee Records Added Failed");
redirect('employee');
}
}
//List of Employees
public function list_employees()
{
$data['employee']=$this->employee_model->get_employee();
$this->load->view('header');
$this->load->view('list_of_employees',$data);
$this->load->view('login/footer');
}
//List of Employees
public function viewlist_employees()
{
$data['employee']=$this->employee_model->get_employee();
$this->load->view('header');
$this->load->view('viewlist_of_employees',$data);
$this->load->view('login/footer');
}
public function delete_employee()
{
$id=$this->input->post('id');
$data=array('status'=>0);
$result=$this->employee_model->delete_employee($id,$data);
if($result==true)
{
$this->session->set_flashdata('msg1',"Deleted Successfully");
redirect('employee/list_employees');
}
else
{
$this->session->set_flashdata('msg1',"Employee Records Deletion Failed");
redirect('employee/list_employees');
}
}
public function edit_employee()
{
$id=$this->uri->segment(3);
$data['employee']=$this->employee_model->edit_employee($id);
$this->load->view('header',$data);
$this->load->view('edit_employee');
}
public function update_employee()
{
$id=$this->input->post('id');
$data=array('name'=>$this->input->post('name'),
'LanID'=>$this->input->post('LanID'),
'reason'=>$this->input->post('reason'),
'PepNumber'=>$this->input->post('PepNumber'),
'Employee_Number'=>$this->input->post('Employee_Number'),
'department'=>$this->input->post('department'),
'status'=>1);
$result=$this->employee_model->update_employee($data,$id);
if($result==true)
{
$this->session->set_flashdata('msg',"Employee Records Updated Successfully");
redirect('employee/list_employees');
}
else
{
$this->session->set_flashdata('msg1',"No changes Made in Employee Records");
redirect('employee/list_employees');
}
}
}
?>
login_database model
<?php
Class Login_Database extends CI_Model {
// Insert registration data in database
public function registration_insert($data) {
// Query to check whether username already exist or not
$condition = "user_name =" . "'" . $data['user_name'] . "'";
$this->db->select('*');
$this->db->from('user_login');
$this->db->where($condition);
$this->db->limit(1);
$query = $this->db->get();
if ($query->num_rows() == 0) {
// Query to insert data in database
$this->db->insert('user_login', $data);
if ($this->db->affected_rows() > 0) {
return true;
}
} else {
return false;
}
}
// Read data using username and password
public function login($data) {
$condition = "user_name =" . "'" . $data['username'] . "' AND " . "user_password =" . "'" . $data['password'] . "'";
$this->db->select('*');
$this->db->from('user_login');
$this->db->where($condition);
$this->db->limit(1);
$query = $this->db->get();
if ($query->num_rows() == 1) {
return true;
} else {
return false;
}
}
// Read data from database to show data in admin page
public function read_user_information($sess_array) {
$condition = "user_name =" . "'" . $sess_array['username'] . "'";
$this->db->select('*');
$this->db->from('user_login');
$this->db->where($condition);
$this->db->limit(1);
$query = $this->db->get();
if ($query->num_rows() == 1) {
return $query->result();
} else {
return false;
}
}
}
?>
employee_model
<?php
class Employee_model extends CI_Model
{
public function insert_employee($data)
{
$this->db->insert('employee_list',$data);
return ($this->db->affected_rows() != 1 ) ? false:true;
}
public function get_employee()
{
$this->db->select('*');
$this->db->from('employee_list');
$this->db->where('status',1);
$query =$this->db->get();
return $query->result();
}
public function delete_employee($id,$data)
{
$this->db->where('id',$id);
$this->db->update('employee_list',$data);
return ($this->db->affected_rows() != 1 ) ? false:true;
}
public function edit_employee($id)
{
$this->db->select('*');
$this->db->from('employee_list');
$this->db->where('id',$id);
$this->db->where('status',1);
$query =$this->db->get();
return $query->result();
}
public function update_employee($data,$id)
{
$this->db->where('id',$id);
$this->db->update('employee_list',$data);
return ($this->db->affected_rows() != 1 ) ? false:true;
}
}
add if statement with logged_in and a redirect to login form if it
is incorrect
public function index()
{
if($this->session->userdata('logged_in'))
{
$this->load->view('header');
$this->load->view('employee');
$this->load->view('login/footer');
}else{
redirect('user_authentication/user_login_show');
}
}
Best Practice is to add the check in the constructor of your controller in CI.
here is the example of mine.
public function __construct() {
parent::__construct();
if (!$this->session->userdata('user_data')) {
return redirect('login');
}
$this->load->model('customer_model');
}
you can add the else statement to redirect to the dashboard or what the resulting page if user is logged in.
Add this line of code to your constructors:
$this->load->library('session');
This will help you.
public function login()
{
$this->load->view('login');
if (isset($_POST['login']))
{
$emailid = $this->input->post('emailid');
$password = $this->input->post('password');
$this->load->model('main_model');
if($this->main_model->can_login('$emailid','$Password'))
{
$session_data = array(
'emailid' => $emailid,
'password' => $password,
'iss_logged_in' => 1
);
$this->session->set_userdata($session_data);
redirect(base_url().'index.php/Hello_cnt/');
}
else
{
$this->session->set_flashdata('error', 'Invalid Username and Password');
redirect(base_url().'index.php/Hello_cnt/login');
}
}
}