Guest User

Untitled

a guest
Dec 5th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.78 KB | None | 0 0
  1. class Simplelogin
  2. {
  3.     var $CI;
  4.     var $user_table = 'users';
  5.  
  6.     function __construct(){
  7.         //$this->CI =& get_instance();
  8.     }
  9.  
  10.     /**
  11.      * Create a user account
  12.      *
  13.      * @access    public
  14.      * @param    string
  15.      * @param    string
  16.      * @param    bool
  17.      * @return    bool
  18.      */
  19.     function create($user = '', $password = '', $auto_login = true) {
  20.         //Put here for PHP 4 users
  21.         $this->CI =& get_instance();        
  22.  
  23.         //Make sure account info was sent
  24.         if($user == '' OR $password == '') {
  25.             return false;
  26.         }
  27.        
  28.         //Check against user table
  29.         $this->CI->db->where('username', $user);
  30.         $query = $this->CI->db->getwhere($this->user_table);
  31.        
  32.         if ($query->num_rows() > 0) {
  33.             //username already exists
  34.             return false;
  35.            
  36.         } else {
  37.             //Encrypt password
  38.             $password = md5($password);
  39.            
  40.             //Insert account into the database
  41.             $data = array(
  42.                         'username' => $user,
  43.                         'password' => $password
  44.                     );
  45.             $this->CI->db->set($data);
  46.             if(!$this->CI->db->insert($this->user_table)) {
  47.                 //There was a problem!
  48.                 return false;                        
  49.             }
  50.             $user_id = $this->CI->db->insert_id();
  51.            
  52.             //Automatically login to created account
  53.             if($auto_login) {        
  54.                 //Destroy old session
  55.                 $this->CI->session->sess_destroy();
  56.                
  57.                 //Create a fresh, brand new session
  58.                 $this->CI->session->sess_create();
  59.                
  60.                 //Set session data
  61.                 $this->CI->session->set_userdata(array('id' => $user_id,'username' => $user));
  62.                
  63.                 //Set logged_in to true
  64.                 $this->CI->session->set_userdata(array('logged_in' => true));            
  65.            
  66.             }
  67.            
  68.             //Login was successful            
  69.             return true;
  70.         }
  71.  
  72.     }
  73.  
  74.     /**
  75.      * Delete user
  76.      *
  77.      * @access    public
  78.      * @param integer
  79.      * @return    bool
  80.      */
  81.     function delete($user_id) {
  82.         //Put here for PHP 4 users
  83.         $this->CI =& get_instance();
  84.        
  85.         if(!is_numeric($user_id)) {
  86.             //There was a problem
  87.             return false;            
  88.         }
  89.  
  90.         if($this->CI->db->delete($this->user_table, array('id' => $user_id))) {
  91.             //Database call was successful, user is deleted
  92.             return true;
  93.         } else {
  94.             //There was a problem
  95.             return false;
  96.         }
  97.     }
  98.  
  99.  
  100.     /**
  101.      * Login and sets session variables
  102.      *
  103.      * @access    public
  104.      * @param    string
  105.      * @param    string
  106.      * @return    bool
  107.      */
  108.     function login($user = '', $password = '') {
  109.         //Put here for PHP 4 users
  110.         $this->CI =& get_instance();        
  111.  
  112.         //Make sure login info was sent
  113.         if($user == '' OR $password == '') {
  114.             return false;
  115.         }
  116.  
  117.         //Check if already logged in
  118.         if($this->CI->session->userdata('username') == $user) {
  119.             //User is already logged in.
  120.             return false;
  121.         }
  122.        
  123.         //Check against user table
  124.         $this->CI->db->where('username', $user);
  125.         $query = $this->CI->db->getwhere($this->user_table);
  126.        
  127.         if ($query->num_rows() > 0) {
  128.             $row = $query->row_array();
  129.            
  130.             //Check against password
  131.             if(md5($password) != $row['password']) {
  132.                 return false;
  133.             }
  134.            
  135.             //Destroy old session
  136.             $this->CI->session->sess_destroy();
  137.            
  138.             //Create a fresh, brand new session
  139.             $this->CI->session->sess_create();
  140.            
  141.             //Remove the password field
  142.             unset($row['password']);
  143.            
  144.             //Set session data
  145.             $this->CI->session->set_userdata($row);
  146.            
  147.             //Set logged_in to true
  148.             $this->CI->session->set_userdata(array('logged_in' => true));            
  149.            
  150.             //Login was successful            
  151.             return true;
  152.         } else {
  153.             //No database result found
  154.             return false;
  155.         }    
  156.  
  157.     }
  158.  
  159.     /**
  160.      * Logout user
  161.      *
  162.      * @access    public
  163.      * @return    void
  164.      */
  165.     function logout() {
  166.         //Put here for PHP 4 users
  167.         $this->CI =& get_instance();        
  168.  
  169.         //Destroy session
  170.         $this->CI->session->sess_destroy();
  171.     }
  172. }
  173. ?>
Add Comment
Please, Sign In to add comment