Guest User

Untitled

a guest
Jan 5th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * Simplelogin Class
  4. *
  5. * Makes authentication simple
  6. *
  7. * Simplelogin is released to the public domain
  8. * (use it however you want to)
  9. *
  10. * Simplelogin expects this database setup
  11. * (if you are not using this setup you may
  12. * need to do some tweaking)
  13. *
  14.  
  15. #This is for a MySQL table
  16. CREATE TABLE `users` (
  17. `id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
  18. `username` VARCHAR( 64 ) NOT NULL ,
  19. `password` VARCHAR( 64 ) NOT NULL ,
  20. UNIQUE (
  21. `username`
  22. )
  23. );
  24.  
  25. *
  26. */
  27. class Simplelogin
  28. {
  29. var $CI;
  30. var $user_table = 'users';
  31.  
  32. function Simplelogin()
  33. {
  34. // get_instance does not work well in PHP 4
  35. // you end up with two instances
  36. // of the CI object and missing data
  37. // when you call get_instance in the constructor
  38. //$this->CI =& get_instance();
  39. }
  40.  
  41. /**
  42. * Create a user account
  43. *
  44. * @access public
  45. * @param string
  46. * @param string
  47. * @param bool
  48. * @return bool
  49. */
  50. function create($user = '', $password = '', $auto_login = true) {
  51. //Put here for PHP 4 users
  52. $this->CI =& get_instance();
  53.  
  54. //Make sure account info was sent
  55. if($user == '' OR $password == '') {
  56. return false;
  57. }
  58.  
  59. //Check against user table
  60. $this->CI->db->where('username', $user);
  61. $query = $this->CI->db->getwhere($this->user_table);
  62.  
  63. if ($query->num_rows() > 0) {
  64. //username already exists
  65. return false;
  66.  
  67. } else {
  68. //Encrypt password
  69. $password = md5($password);
  70.  
  71. //Insert account into the database
  72. $data = array(
  73. 'username' => $user,
  74. 'password' => $password
  75. );
  76. $this->CI->db->set($data);
  77. if(!$this->CI->db->insert($this->user_table)) {
  78. //There was a problem!
  79. return false;
  80. }
  81. $user_id = $this->CI->db->insert_id();
  82.  
  83. //Automatically login to created account
  84. if($auto_login) {
  85. //Destroy old session
  86. $this->CI->session->sess_destroy();
  87.  
  88. //Create a fresh, brand new session
  89. $this->CI->session->sess_create();
  90.  
  91. //Set session data
  92. $this->CI->session->set_userdata(array('id' => $user_id,'username' => $user));
  93.  
  94. //Set logged_in to true
  95. $this->CI->session->set_userdata(array('logged_in' => true));
  96.  
  97. }
  98.  
  99. //Login was successful
  100. return true;
  101. }
  102.  
  103. }
  104.  
  105. /**
  106. * Delete user
  107. *
  108. * @access public
  109. * @param integer
  110. * @return bool
  111. */
  112. function delete($user_id) {
  113. //Put here for PHP 4 users
  114. $this->CI =& get_instance();
  115.  
  116. if(!is_numeric($user_id)) {
  117. //There was a problem
  118. return false;
  119. }
  120.  
  121. if($this->CI->db->delete($this->user_table, array('id' => $user_id))) {
  122. //Database call was successful, user is deleted
  123. return true;
  124. } else {
  125. //There was a problem
  126. return false;
  127. }
  128. }
  129.  
  130.  
  131. /**
  132. * Login and sets session variables
  133. *
  134. * @access public
  135. * @param string
  136. * @param string
  137. * @return bool
  138. */
  139. function login($user = '', $password = '') {
  140. //Put here for PHP 4 users
  141. $this->CI =& get_instance();
  142.  
  143. //Make sure login info was sent
  144. if($user == '' OR $password == '') {
  145. return false;
  146. }
  147.  
  148. //Check if already logged in
  149. if($this->CI->session->userdata('username') == $user) {
  150. //User is already logged in.
  151. return false;
  152. }
  153.  
  154. //Check against user table
  155. $this->CI->db->where('username', $user);
  156. $query = $this->CI->db->getwhere($this->user_table);
  157.  
  158. if ($query->num_rows() > 0) {
  159. $row = $query->row_array();
  160.  
  161. //Check against password
  162. if(md5($password) != $row['password']) {
  163. return false;
  164. }
  165.  
  166. //Destroy old session
  167. $this->CI->session->sess_destroy();
  168.  
  169. //Create a fresh, brand new session
  170. $this->CI->session->sess_create();
  171.  
  172. //Remove the password field
  173. unset($row['password']);
  174.  
  175. //Set session data
  176. $this->CI->session->set_userdata($row);
  177.  
  178. //Set logged_in to true
  179. $this->CI->session->set_userdata(array('logged_in' => true));
  180.  
  181. //Login was successful
  182. return true;
  183. } else {
  184. //No database result found
  185. return false;
  186. }
  187.  
  188. }
  189.  
  190. /**
  191. * Logout user
  192. *
  193. * @access public
  194. * @return void
  195. */
  196. function logout() {
  197. //Put here for PHP 4 users
  198. $this->CI =& get_instance();
  199.  
  200. //Destroy session
  201. $this->CI->session->sess_destroy();
  202. }
  203. }
  204. ?>
Add Comment
Please, Sign In to add comment