blueset

Controller

May 17th, 2014
448
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.11 KB | None | 0 0
  1. <?php
  2. class User extends CI_Controller {
  3.     public function __construct()
  4.     {
  5.         parent::__construct();
  6.         $this->load->library('form_validation');
  7.         $this->load->model('user_model');
  8.         $this->load->helper('url');
  9.         $this->load->helper('form');
  10.         $this->load->helper('cookie');
  11.     }
  12.     function login(){
  13.  
  14.         $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|callback_username_check');
  15.         $this->form_validation->set_rules('password', 'Password', 'trim|required|md5|callback_password_check');
  16.         $this->_username = $this->input->post('username');              
  17.        
  18.        
  19.         $remember_me = $this->input->post('remember_me');
  20.         $json_string = $this->input->cookie('userinfo');
  21.         $userinfo_json = json_decode($json_string);
  22.        
  23.         if(isset($userinfo_json->username)){
  24.             if ($this->username_check($userinfo_json->username)){
  25.                 $this->user_model->login($userinfo);
  26.                 redirect('admin/dashboard');
  27.             }
  28.         }
  29.        
  30.        
  31.         if ($this->form_validation->run() == FALSE){
  32.             $this->load->view('account/login');
  33.         } else {
  34.             $userinfo=$this->user_model->get_by_username($this->_username);
  35.             $this->user_model->login($userinfo);
  36.             if($remember_me=="on"){$this->user_model->write_session($userinfo);}
  37.             redirect('admin/dashboard');
  38.         }
  39.        
  40.        
  41.     }
  42.     function username_check($username){
  43.         if ($this->user_model->get_by_username($username)){
  44.             return TRUE;
  45.         }else{
  46.             $this->form_validation->set_message('username_check', 'User name not exist.');
  47.             return FALSE;
  48.         }
  49.     }
  50.     function password_check($password) {
  51.         $password = md5($password);
  52.         if ($this->user_model->password_check($this->_username, $password)){
  53.             return TRUE;
  54.         }else{
  55.             $this->form_validation->set_message('password_check', 'Incorrect username or paswsword.');
  56.             return FALSE;
  57.         }
  58.      }
  59.     function register(){
  60.             $rule=array(                  
  61.             array(
  62.                 'field'=>'username',
  63.                 'label'=>'User name',
  64.                 'rules'=>'trim|required|xss_clean|callback_username_exists'
  65.             ),
  66.             array(
  67.                 'field'=>'password',
  68.                 'label'=>'Paassword',
  69.                 'rules'=>'trim|required|min_length[4]|max_length[12]|matches[passwordconf]|md5|xss_clean'
  70.             ),
  71.             array(
  72.                 'field'=>'email',
  73.                 'label'=>'E-mail address',
  74.                 'rules'=>'trim|required|xss_clean|valid_email|callback_email_exists'
  75.             )
  76.         );
  77.          $this->form_validation->set_rules($rule);
  78.  
  79.         $this->form_validation->set_error_delimiters('<span class="error">', '</span>');
  80.        
  81.         if ($this->form_validation->run() == FALSE) {
  82.             $this->load->view('account/register');
  83.         }else{
  84.             $username = $this->input->post('username');
  85.             $password = md5($this->input->post('password'));
  86.             $email = $this->input->post('email');
  87.             $display_name = $this->input->post('display_name');
  88.             if ($this->user_model->add_user($username, $password, $email, $display_name)){
  89.                 $data['message'] = "The user account has now been created! You can go "
  90.                             .anchor('account/index', 'here').'.';
  91.                 $data['here'] = anchor("account/index", 'here');
  92.             }else{
  93.                 $data['message'] = "There was a problem when adding your account. You can register "
  94.                             .anchor('account/register', 'here').' again.';
  95.                 $data['here'] = anchor("account/register", 'here');
  96.             }
  97.             $this->load->view('account/note', $data);
  98.         };        
  99.        }
  100.    
  101.     function username_exists($username){
  102.         if ($this->user_model->get_by_username($username)){
  103.             $this->form_validation->set_message('username_exists', 'User name is not available.');
  104.             return FALSE;
  105.         }
  106.         return TRUE;
  107.     }
  108.     function email_exists($email){
  109.         if ($this->user_model->email_exists($email)){
  110.             $this->form_validation->set_message('email_exists', 'E-mail is not available.');
  111.             return FALSE;
  112.         }
  113.         return TRUE;
  114.     }
  115.     function logout(){
  116.         delete_cookie("userinfo");
  117.         if ($this->user_model->logout() == TRUE){
  118.              $data['message']='Log out successfully!';
  119.             $this->load->view('account/login',$data);
  120.         }else{
  121.              redirect('/', 'refresh');
  122.         }
  123.     }
  124.     function cookie_test()
  125.     {
  126.         $cookie = array(
  127.             'name'   => 'testCookie',
  128.             'value'  => "asjhjfajbfuwebhfgewyl",
  129.             'expire' => '2592000',
  130.             'secure' => TRUE
  131.         );
  132.         //$result = $this->input->set_cookie($cookie);
  133.         $a = get_cookie('testCookie');
  134.         $b = $this->input->cookie();
  135.         var_dump($a);
  136.         var_dump($b);
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment