<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends CI_Controller {
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/user
* - or -
* http://example.com/index.php/user/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* @see https://codeigniter.com/user_guide/general/urls.html
*/
public function __construct(){
parent::__construct();
if(($this->session->userdata('id')!='') || ($this->session->userdata('IsLogin')===TRUE))
{
if($this->session->userdata('role')=='admin'){
redirect(site_url('admin'));
}
}
$this->output->set_header('Last-Modified: ' . gmdate("D, d M Y H:i:s") . ' GMT');
$this->output->set_header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
$this->output->set_header('Pragma: no-cache');
$this->output->set_header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
}
/*Function for Login*/
public function index() {
if(isset($_POST['login'])){
$loginid=$this->input->post('username');
$password=$this->input->post('password');
$where=array('name'=>$loginid,'phone'=>$password);
$data['allinfo']=($this->user_model->get_joins('users',$where));
if(empty($data['allinfo']))
{
$this->session->set_flashdata('message', 'Please try again. Username or password incorrect.');
}
else
{
$session=array(
'id'=>$data['allinfo'][0]['id'],
'name'=>$data['allinfo'][0]['name'],
'role'=>$data['allinfo'][0]['extra'],
'IsLogin'=>TRUE,
);
if($data['allinfo'][0]['extra'] == "admin")
{
$this->session->set_userdata($session);
redirect(site_url('admin'));
}
else{
redirect(site_url(''));
}
}
}
$this->load->view('login');
}
}
?>
login.php (view)
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<style>
@import "bourbon";
.wrapper {
margin-top: 80px;
margin-bottom: 80px;
}
.form-signin {
max-width: 480px;
padding: 15px 35px 45px;
margin: 0 auto;
background-color: #fff;
border: 1px solid rgba(0,0,0,0.1);
.form-signin-heading,
.checkbox {
margin-bottom: 30px;
}
.checkbox {
font-weight: normal;
}
.form-control {
position: relative;
font-size: 16px;
height: auto;
padding: 10px;
@include box-sizing(border-box);
&:focus {
z-index: 2;
}
}
input[type="text"] {
margin-bottom: -1px;
border-bottom-left-radius: 0;
border-bottom-right-radius: 0;
}
input[type="password"] {
margin-bottom: 20px;
border-top-left-radius: 0;
border-top-right-radius: 0;
}
}
</style>
<div class="wrapper">
<div class="main-loginform">
<form class="form-signin" name="signUp" method="post" id="myform">
<?php if(isset($_SESSION['message'])){ ?>
<div class="alert alert-success success-msg">
<span><?php echo $_SESSION['message'];$_SESSION['message']="";?></span>
</div>
<?php } ?>
<div class="heading_login"><h2 class="form-signin-heading">Login</h2><div>
<label class="title">Username</label>
<div class=""><input type="text" class="form-control" name="username" placeholder="Username" required></div>
<div> </div>
<label class="title">Password</label>
<div class=""><input type="password" class="form-control" name="password" placeholder="Password" required></div>
<label class="checkbox">
</label>
<button class="btn btn-lg btn-primary btn-block" type="submit" name='login'>Login</button>
</form>
</div>
</div>
<script>
$(document).ready(function(){
setTimeout( "jQuery('.success-msg').hide();",3000 );
})
</script>
Admin.php <?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Admin extends CI_Controller
{
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/user
* - or -
* http://example.com/index.php/user/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* @see https://codeigniter.com/user_guide/general/urls.html
*/
public function __construct()
{
parent::__construct();
if (($this->session->userdata('id') == '') || ($this->session->userdata('IsLogin') === FALSE)) {
redirect(site_url('login'));
}
$this->output->set_header('Last-Modified: ' . gmdate("D, d M Y H:i:s") . ' GMT');
$this->output->set_header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
$this->output->set_header('Pragma: no-cache');
$this->output->set_header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
}
/*Function for Logout */
public function logout(){
$newdata = array(
'id' => '',
'name' => '',
'role' => '',
'IsLogin' => FALSE
);
//session destroy
$this->session->unset_userdata($newdata);
$this->session->sess_destroy();
redirect(base_url());
}
/*Function for Dashboard*/
public function index()
{
$join=array(array('table'=>'user_address','condition'=>'user_address.userid=users.id','jointype'=>'left'));
$fields=array(
"users.*",
"user_address.address1",
"user_address.address2",
"user_address.city",
"user_address.state",
"user_address.country",
"user_address.pincode",
);
$iswhere = array(
'user_address.isprimary' => 'Y',
'users.extra !=' => 'admin'
);
$data['users'] =$this->user_model->get_joins('users',$iswhere,$join,$fields);
$this->load->view('dashboard',$data);
}
}
?>
User_model.php
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class User_model extends CI_Model {
public function __construct() {
parent::__construct();
}
//**---FOR INSERT DATA
public function INSERTDATA($tablename, $feild = '') {
if (!empty($tablename) || !empty($feild)):
$this->db->set($feild);
$insert = $this->db->insert($tablename);
if ($insert):
return $this->db->insert_id();
endif;
else: return "Invalid Input Provided";
endif;
}
//**---FOR UPDATE DATA
public function UPDATEDATA($tablename, $where = '', $feild = '') {
if (!empty($tablename) || !empty($feild)):
$this->db->where($where);
$this->db->update($tablename, $feild);
return TRUE;
else: return FALSE;
endif;
}
//**---FOR DELETE DATA
public function DELETEDATA($tablename = '', $where = '') {
if (!empty($tablename) || !empty($where)):
$this->db->where($where);
$this->db->delete($tablename);
return TRUE;
else: return FALSE;
endif;
}
//**---FOR SELECT DATA
public function get_sql_select_data($tablename = '', $where = '', $order_by = '', $where_not='', $feild = '', $limit = '', $like = '') {
if (!empty($feild))
$this->db->select($feild);
if (empty($feild))
$this->db->select();
if (!empty($where))
$this->db->where($where);
if (!empty($limit))
$this->db->limit($limit);
if (!empty($where_not))
$this->db->where_not_in($where_not);
if (!empty($like))
$this->db->like($like);
if (!empty($order_by))
$this->db->order_by($order_by);
$this->db->from($tablename);
$query = $this->db->get();
return $query->result_array();
}
//**---JOIN DATA
public function get_joins($tablename = '', $where = '', $joins = '', $columns = '', $like = '', $group_by = '', $order_by = '', $limit = '', $start = '') {
if(!empty($columns))$this->db->select($columns);
if(empty($columns))$this->db->select('*');
if (is_array($joins) && count($joins) > 0) {
foreach ($joins as $k => $v) {
$this->db->join($v['table'], $v['condition'], $v['jointype']);
}
}
if (!empty($group_by))
$this->db->group_by($group_by);
if (!empty($like))
$this->db->or_like($like);
if (!empty($limit))
$this->db->limit($limit, $start);
if (!empty($where))
$this->db->where($where);
if (!empty($order_by))
$this->db->order_by($order_by);
$this->db->from($tablename);
$query = $this->db->get();
return $query->result_array();
}
function getSumOfValues($tbl_nm,$startdate,$enddate,$coulamNm,$columnval,$noSum,$type){
foreach($noSum as $k=>$v)
$query = $this->db->select_avg($k,$v);
$this->db->where('date_time >="'.$startdate.'" AND date_time <="'.$enddate.'" AND '.$coulamNm.'="'.$columnval.'"');
$query = $this->db->get($tbl_nm);
if($type == '1'){
$sumData = array_shift($query->result_array());
$sumByCOMs = '';
$c = 1;
foreach($noSum as $k=>$v){
if($c == 1){
$sumByCOMs = round($sumData[$v]);
}else{
$sumByCOMs = $sumByCOMs.','.round($sumData[$v]);
}
$c++;}
return $sumByCOMs;
}else
return $query->result_array();
}
}
?>
Sign up here with your email
ConversionConversion EmoticonEmoticon