• logo
  • PHP
  • PHP OOPs
  • script
    • JavaScript
    • JQuery
    • Ajax
    • AngularJs
    • VueJs
    • NodeJs
    • ReactJs
  • wordpress
  • Codeigniter
  • Codeigniter 4
  • Laravel
  • Python
  • MySql
  • Json
  • C
  • C++
  • More ...
    • Cakephp Framework
    • MongoDb
    • MySqli PDO
    • .htaccess
    • HTML 5
    • CSS
    • SEO
    • DCA

Codeigniter Tutorials

  • What is codeigniter?
  • Application_Architecture
  • MVC Architecture
  • HMVC Architecture
  • Codeigniter Configuration
  • Remove index.php from url in codeigniter
  • MVC Concept
  • View
  • Alternate PHP Syntax for View Files
  • Routing
  • Codeigniter URL
  • Get Current URL
  • Previous page URL get
  • Seo Friendly URL
  • Slug Create in codeigniter
  • What is _remap() function
  • Remove controller name from url in codeigniter
  • Codeigniter Controller Class
  • Class Constructor
  • GET $ POST method in Codeigniter
  • Models
  • Basepath, Apppath, FCPATH
  • URI Segment
  • Page Redirect
  • Helper class
  • Custom Helper class
  • Form Helper
  • Common Helper Functions
  • Common Function
  • Array Problems
  • Call controller in Helper
  • Add active class to menu using Helper class
  • Custom Library
  • Custom Library Example
  • when to use get_instance()
  • Codeigniter Hook
  • how to work inline css in codeigniter
  • Custom 404 page
  • 404 custom error page
  • Create custom config file in codeigniter
  • How to set and get config item value
  • How to Speed Up CodeIgniter App?
  • Codeigniter Functions
  • Session
  • cookies
  • How to Set & Get Tempdata in Codeigniter
  • flash messages in Codeigniter
  • Flashdata
  • Encryption and Decryption In CodeIgniter
  • Codeigniter security
  • csrf token form security
  • Password Hashing
  • Form Validation
  • Custom Validation
  • Registration Form with validation
  • Server Side Form Validation
  • Validate Select Option Field
  • Date Format Validation
  • Date Format change in codeigniter
  • Date Functions
  • DOB Validation
  • CI CRUD
  • User SignUp
  • User Login
  • User Logout
  • Login Account
  • Login form with RememberMe
  • Login Form with session
  • User change password
  • Change Password with Callback Validation to Check Old Password
  • Forgot password
  • Reset password
  • Insert data in database
  • Fetch data from database
  • Update data in database
  • Delete data in database
  • File Upload
  • Image Upload with resize Image
  • Upload Multiple file and images
  • Upload Multiple images with CRUD
  • File and image update
  • Upload Image Using Ajax.
  • Email Send
  • Email Send Using Email library
  • Email Send Using SMTP Gmail
  • Notification send
  • store data in json format in DB
  • Json parse
  • Fetch data Using Ajax with Json data
  • How to Show data Using Ajax with Json parse
  • Get JSON Data from PHP Script using jQuery Ajax
  • Insert data Using Ajax
  • Submit data Using Ajax with form validation
  • How to show data Using Ajax in codeigniter
  • Insert & Update Using Ajax
  • Registration Form With Validation Using Ajax in codeigniter
  • Delete data Using Ajax Confirmation
  • Delete All data Using checkbox selection
  • Ajax CSRF Token
  • Ajax Post
  • Ajax serverside form validation
  • Contact form using AJAX with form validation
  • DataTable Using Ajax dynamically
  • DataTables pagination using AJAX with Custom filter
  • DataTables AJAX Pagination with Search and Sort in codeigniter
  • DataTables in Codeigniter using Ajax
  • Ajax Custom Serarch
  • Ajax Live Data Search using Jquery PHP MySql
  • Ajax Custom Serarch and sorting in datatable
  • Dynamic Search Using Ajax
  • Autocomplete using jquery ajax
  • Jquery Ajax Autocomplete Search using Typeahead
  • Dynamic Dependent Dropdown Using Ajax
  • Dynamic Dependent Dropdown list Using Ajax
  • Dynamic Dependent Dropdown in codeigniter using Ajax
  • ajax username/email availability check using JQuery
  • Check Email Availability Using Ajax
  • Data Load on mouse scroll
  • Ajax CI Pagination
  • Pagination in codeigniter
  • Ajax Codeigniter Pagination
  • email exists or not using ajax with json
  • CRUD using AJAX With Modal popup in CI
  • Add / Show Data on modal popup using Ajax
  • Modal popup Validation using Ajax
  • Data show on Modal popup Using Ajax
  • Add / Remove text field dynamically using jquery ajax
  • How to Add/Delete Multiple HTML Rows using JavaScript
  • Delete Multiple Rows using Checkbox
  • Multiple Checkbox value
  • Form submit using jquery Example
  • REST & SOAP API
  • Multi-Language implementation in CodeIgniter
  • How to pass multiple array in view
  • Captcha
  • create zip file and download
  • PhpOffice PhpSpreadsheet Library (Export data in excel sheet)
  • data export in excel sheet
  • Excel File generate in Codeigniter using PHPExcel
  • Dompdf library
  • tcpdf library
  • Html table to Excel & docs download
  • CI Database Query
  • Database Query
  • SQL Injection Prevention
  • Auth Model
  • Join Mysql
  • Tree View in dropdown option list
  • OTP Integration in codeigniter
  • curl post
  • download file using curl
  • Sweet Alert
  • Sweet alert Delete & Success
  • Log Message in Codeigniter
  • Menu & Submenu show dynamically
  • Set Default value in input box
  • Cron Jobs
  • Stored Procedure
  • Display Loading Image when AJAX call is in Progress
  • Send SMS
  • IP Address
  • Codeigniter Tutorialspoint
  • Website Link
  • How To Create Dynamic Xml Sitemap In Codeigniter
  • Paypal Payment Integration
  • Get Latitude and Longitude From Address in Codeigniter Using google map API
  • How To Create Simple Bar Chart In Codeigniter Using AmCharts?
  • dynamic Highcharts in Codeigniter
  • Barcode in Codeigniter
  • Codeigniter Interview Questions
  • Project
Home » Codeigniter » 

How to create custom library in codeigniter?


Create Auth Library

File Name : Auth.php

<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
class Auth
{
var $CI;
var $session_expire = 600;

public function __construct()
{
// parent::__construct();
// remove the parent::__construct() from your library constructor, because it's not extending anything so has no parent to call.
// $CI =& get_instance(); // if $CI is not declare instance variable
$this->CI =& get_instance();
$this->CI->load->database();
$this->CI->load->library('encryption');
$this->CI->load->helper('url');


}

function login_admin($userid, $password, $remember=false)
{
/* $this->CI->db->select('*');
$this->CI->db->where('user_id', $userid);
$this->CI->db->where('password', ($password));
$this->CI->db->limit(1);
$query = $this->CI->db->get('admin_info');
if ($query->num_rows() > 0) {
return $query->result();
} else {
return false;
}*/

$this->CI->db->select('*');
$this->CI->db->where('userid', $userid);
$this->CI->db->where('password', ($password));
$this->CI->db->limit(1);
$result = $this->CI->db->get('admin');
$result = $result->row_array();

if (sizeof($result) > 0)
{

$admin['id'] = $result['id'];
$admin['userid'] = $result['userid'];
$admin['email'] = $result['email'];
$this->CI->session->set_userdata('logdinUser',$admin);

/* $admin = array(
'id' => $result['id'],
'userid' => $result['userid'],
'email' => $result['email'],
'loginuser' => TRUE
);
$this->CI->session->set_userdata($admin);
*/

if(!$remember)
{
$admin['admin']['expire'] = time()+$this->session_expire;
}
else
{
$admin['admin']['expire'] = false;
}

return true;
}
else
{
return false;
}



}


/* ***************** Logout ************************** */
public function logout()
{

$this->CI->session->unset_userdata('logdinUser');
$this->CI->session->sess_destroy();

}

/* **************End Logout ****************** */


/* **************** check login exist or not ********************** */

function is_logged_in($redirect = false, $default_redirect = true)
{

$admin = $this->CI->session->userdata('logdinUser');
if (!$admin) // if user not logged in.
{
if ($redirect)
{
$this->CI->session->set_flashdata('redirect', $redirect);
}

if ($default_redirect)
{
redirect($this->CI->config->item('admin_folder').'/login');
//redirect('admin/login');
}

return false;

}
else
{

// if user is logged in.
//check if the session is expired if not reset the timer
if($admin['expire'] && $admin['expire'] < time())
{



$this->logout();
if($redirect)
{
$this->CI->session->set_flashdata('redirect', $redirect);
}

if($default_redirect)
{
redirect('admin/login');
}

return false;
}
else
{

//update the session expiration to last more time if they are not remembered
if($admin['expire'])
{
$admin['expire'] = time()+$this->session_expire;
$this->CI->session->set_userdata(array('admin'=>$admin));
}
}


return true;
}
}

/* **************** end check login exist or not ********************** */




/* ********************** Reset Password ******************************* */

function reset_password($email)
{
$admin = $this->get_admin_by_email($email);

if ($admin)
{
$this->CI->load->helper('string');
$this->CI->load->library('email');
$new_password = random_string('alnum', 8);
//$admin['password'] = sha1($new_password);
$admin['password'] = $new_password;
$this->save_admin($admin);

/*
$this->CI->email->from($this->CI->config->item('email'), $this->CI->config->item('site_name'));
$this->CI->email->to($email);
$this->CI->email->subject($this->CI->config->item('site_name').': Admin Password Reset');
$this->CI->email->message('Your password has been reset to '. $new_password .'.');
//$this->CI->email->send();
*/

/*
$this->load->library('email');
$this->email->clear(TRUE);
$this->email->from($this->CI->config->item('email'), 'Your Name');
$this->email->to($email);
//$this->email->cc('another@another-example.com');
//$this->email->bcc('them@their-example.com');
$this->email->subject($this->CI->config->item('site_name').': Admin Password Reset');
$this->email->message('Your password has been reset to '. $new_password .'.');
$this->email->send();

if ($this->email->send(FALSE))
{
// This method will automatically clear all parameters if the request was successful. To stop this behaviour pass FALSE
}

*/



$this->CI->session->set_flashdata('passmsg','<div class="alert alert-success text-center">Your Password has been sent successfully! Please check your mail.</div>');


return true;
}
else
{
return false;
}
}


/* ************************** End Reset Password ********************************* */



private function get_admin_by_email($email)
{
$this->CI->db->select('*');
$this->CI->db->where('email', $email);
$this->CI->db->limit(1);
$result = $this->CI->db->get('admin');
$result = $result->row_array();

if (sizeof($result) > 0)
{
return $result;
}
else
{
return false;
}
}

/*
This function takes admin array and inserts/updates it to the database
*/
public function save_admin($admin)
{
if ($admin['id'])
{
$this->CI->db->where('id', $admin['id']);
$this->CI->db->update('admin', $admin);
}
else
{
$this->CI->db->insert('admin', $admin);
}
}




/* ******************************************* */

public function change_password($email,$newpass)
{
$admin = $this->get_admin_by_email($email);

if ($admin)
{
$this->CI->load->helper('string');
$this->CI->load->library('email');
//$new_password = random_string('alnum', 8);
//$admin['password'] = sha1($new_password);
$admin['password'] = $newpass;
$this->change_admin_password($admin,$newpass);

$this->CI->session->set_flashdata('passmsg','<div class="alert alert-success text-center">Your Password has been change successfully!</div>');


return true;
}
else
{
return false;
}
}


public function change_admin_password($admin,$newpass)
{
if ($admin['id'])
{
$this->CI->db->where('id', $admin['id']);
$this->CI->db->set('password',$newpass);
$this->CI->db->update('admin', $admin);
}
else
{
$this->CI->db->insert('admin', $admin);
}
}











/*
This function gets a complete list of all admin
*/


function get_admin_list()
{
$this->CI->db->select('*');
$this->CI->db->order_by('userid', 'ASC');
$this->CI->db->order_by('password', 'ASC');
$this->CI->db->order_by('email', 'ASC');
$result = $this->CI->db->get('admin');
$result = $result->result();

return $result;
}



/*
This function gets an individual admin
*/
function get_admin($id)
{
$this->CI->db->select('*');
$this->CI->db->where('id', $id);
$result = $this->CI->db->get('admin');
$result = $result->row();

return $result;
}

function check_id($str)
{
$this->CI->db->select('id');
$this->CI->db->from('admin');
$this->CI->db->where('id', $str);
$count = $this->CI->db->count_all_results();

if ($count > 0)
{
return true;
}
else
{
return false;
}
}

function check_email($str, $id=false)
{
$this->CI->db->select('email');
$this->CI->db->from('admin');
$this->CI->db->where('email', $str);
if ($id)
{
$this->CI->db->where('id !=', $id);
}
$count = $this->CI->db->count_all_results();

if ($count > 0)
{
return true;
}
else
{
return false;
}
}

function delete($id)
{
if ($this->check_id($id))
{
$admin = $this->get_admin($id);
$this->CI->db->where('id', $id);
$this->CI->db->limit(1);
$this->CI->db->delete('admin');

return $admin->firstname.' '.$admin->lastname.' has been removed.';
}
else
{
return 'The admin could not be found.';
}
}


/*
public function add_admin_user($data)
{
$this->CI->db->insert('admin',$data);
}
*/

public function add_admin_user()
{
$userid = $this->CI->input->post('userid');
$password = $this->CI->input->post('password');
$email = $this->CI->input->post('email');
$mobile = $this->CI->input->post('mobile');

$data = array(
'userid' => $userid,
'password' => $password,
'email' => $email,
'mobileno' => $mobile

);

$this->CI->db->insert('admin',$data);

}






}

Create Login Controller

File Name : Login.php

<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller {
function __construct()
{
parent::__construct();
//force_ssl();
$this->load->library('Auth');
$this->load->helper('language');
$this->lang->load('login');
$this->load->helper('form');
}

public function index()
{
$redirect = $this->auth->is_logged_in(false, false);
echo $redirect;
//if they are logged in, we send them back to the dashboard by default, if they are not logging in
if ($redirect)
{
redirect($this->config->item('admin_folder').'/dashboard');
//redirect('admin/dashboard');
}
$this->load->view($this->config->item('admin_folder').'/login');

}


public function logincheck()
{



$userid = $this->input->post('uid');
$password = $this->input->post('pass');
$remember = $this->input->post('remember');
$login = $this->auth->login_admin($userid, $password, $remember);
if ($login > 0)
{
//redirect('admin/dashboard');
redirect($this->config->item('admin_folder').'/dashboard');
}
else
{


$this->session->set_flashdata('error', lang('error_authentication_failed'));
//redirect('admin/login');
redirect($this->config->item('admin_folder').'/login');
}



}

}

Dashboard Controller

File Name : Dashboard.php

<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
class Dashboard extends CI_Controller {
function __construct()
{
parent::__construct();
$loggedIn = $this->session->userdata('logdinUser');
//print_r($loggedIn['id']);
if($loggedIn == TRUE)
{

}
else
{
redirect('admin/login');

}

}

public function index()
{

$this->load->view('admin/header');
$this->load->view('admin/dashboard');
$this->load->view('admin/footer');

}




public function Calendar()
{
$data['getEmployee'] = $this->EmployeeModel->getEmployee();
$data['getHoliday'] = $this->EmployeeModel->getHoliday();
$data['getAttendance'] = $this->EmployeeModel->getAttendance();
$data['getSalary'] = $this->EmployeeModel->getSalary();
$data['getShift'] = $this->EmployeeModel->getShift();
$data['getShiftAttendance'] = $this->EmployeeModel->getShiftAttendance();

$this->load->view('admin/header');
$this->load->view('admin/dashboard',$data);
$this->load->view('admin/footer');
}



}

Logout Controller

File Name : Logout.php

<?php
defined('BASEPATH') OR exit('no script direct access allowed');
class Logout extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library('Auth');
$this->lang->load('login');
$this->load->helper('language');
$loggedIn = $this->session->userdata('logdinUser');
if($loggedIn == TRUE)
{

}
else
{
redirect('admin/login');
}
}

public function index()
{



//$this->session->set_flashdata('message', 'Successfully Logout');
$this->session->set_flashdata('message', lang('message_logged_out'));
//redirect($this->config->item('admin_folder').'/login');
$this->auth->logout();
//redirect('admin/login');
$this->load->view('admin/login');
//redirect('admin/login','refresh');



/* $this->session->unset_userdata('userid');
$this->session->sess_destroy();
$data['message'] = 'Successfully Logout';
$this->load->view('admin/login', $data);
*/

}
}

File Name :


File Name :





Itechtuto

Connect Us Socially:

Quick Links

  • itech
  • About Us
  • Feedback
  • Trademarks
  • Privacy Policy
  • Terms of Use
  • Sitemap
  • Trademarks
  • Privacy Policy
  • Terms of Use
  • Sitemap

Copyright © 2016 itechxpert (P) Ltd. All rights reserved.

Copyright © 2016 itechxpert (P) Ltd. All rights reserved.