hopingsteam

application/controllers/Users.php

Jun 5th, 2017
204
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3.  * User Management class created by CodexWorld
  4.  */
  5. class Users extends CI_Controller {
  6.    
  7.     function __construct() {
  8.         parent::__construct();
  9.         $this->load->library('form_validation');
  10.         $this->load->model('user');
  11.     }
  12.    
  13.     /*
  14.      * User account information
  15.      */
  16.     public function account(){
  17.         $data = array();
  18.         if($this->session->userdata('isUserLoggedIn')){
  19.             $data['user'] = $this->user->getRows(array('id'=>$this->session->userdata('userId')));
  20.             //load the view
  21.             $this->load->view('users/account', $data);
  22.         }else{
  23.             redirect('users/login');
  24.         }
  25.     }
  26.    
  27.     /*
  28.      * User login
  29.      */
  30.     public function login(){
  31.         $data = array();
  32.         if($this->session->userdata('success_msg')){
  33.             $data['success_msg'] = $this->session->userdata('success_msg');
  34.             $this->session->unset_userdata('success_msg');
  35.         }
  36.         if($this->session->userdata('error_msg')){
  37.             $data['error_msg'] = $this->session->userdata('error_msg');
  38.             $this->session->unset_userdata('error_msg');
  39.         }
  40.         if($this->input->post('loginSubmit')){
  41.             $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
  42.             $this->form_validation->set_rules('password', 'password', 'required');
  43.             if ($this->form_validation->run() == true) {
  44.                 $con['returnType'] = 'single';
  45.                 $con['conditions'] = array(
  46.                     'email'=>$this->input->post('email'),
  47.                     'password' => md5($this->input->post('password')),
  48.                     'status' => '1'
  49.                 );
  50.                 $checkLogin = $this->user->getRows($con);
  51.                 if($checkLogin){
  52.                     $this->session->set_userdata('isUserLoggedIn',TRUE);
  53.                     $this->session->set_userdata('userId',$checkLogin['id']);
  54.                     redirect('users/account/');
  55.                 }else{
  56.                     $data['error_msg'] = 'Wrong email or password, please try again.';
  57.                 }
  58.             }
  59.         }
  60.         //load the view
  61.         $this->load->view('users/login', $data);
  62.     }
  63.    
  64.     /*
  65.      * User registration
  66.      */
  67.     public function registration(){
  68.         $data = array();
  69.         $userData = array();
  70.         if($this->input->post('regisSubmit')){
  71.             $this->form_validation->set_rules('name', 'Name', 'required');
  72.             $this->form_validation->set_rules('email', 'Email', 'required|valid_email|callback_email_check');
  73.             $this->form_validation->set_rules('password', 'password', 'required');
  74.             $this->form_validation->set_rules('conf_password', 'confirm password', 'required|matches[password]');
  75.  
  76.             $userData = array(
  77.                 'name' => strip_tags($this->input->post('name')),
  78.                 'email' => strip_tags($this->input->post('email')),
  79.                 'password' => md5($this->input->post('password')),
  80.                 'gender' => $this->input->post('gender'),
  81.                 'phone' => strip_tags($this->input->post('phone'))
  82.             );
  83.  
  84.             if($this->form_validation->run() == true){
  85.                 $insert = $this->user->insert($userData);
  86.                 if($insert){
  87.                     $this->session->set_userdata('success_msg', 'Your registration was successfully. Please login to your account.');
  88.                     redirect('users/login');
  89.                 }else{
  90.                     $data['error_msg'] = 'Some problems occured, please try again.';
  91.                 }
  92.             }
  93.         }
  94.         $data['user'] = $userData;
  95.         //load the view
  96.         $this->load->view('users/registration', $data);
  97.     }
  98.    
  99.     /*
  100.      * User logout
  101.      */
  102.     public function logout(){
  103.         $this->session->unset_userdata('isUserLoggedIn');
  104.         $this->session->unset_userdata('userId');
  105.         $this->session->sess_destroy();
  106.         redirect('users/login/');
  107.     }
  108.    
  109.     /*
  110.      * Existing email check during validation
  111.      */
  112.     public function email_check($str){
  113.         $con['returnType'] = 'count';
  114.         $con['conditions'] = array('email'=>$str);
  115.         $checkEmail = $this->user->getRows($con);
  116.         if($checkEmail > 0){
  117.             $this->form_validation->set_message('email_check', 'The given email already exists.');
  118.             return FALSE;
  119.         } else {
  120.             return TRUE;
  121.         }
  122.     }
  123. }
RAW Paste Data