First part of a login/register script in CodeIgniter


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2.  
  3.  
  4. ## LOGIN and REGISTER form processing and whatnot for Samiia Boutique ##
  5. ## @coder: Andy Abi Haydar ##
  6.  
  7. class Login_register extends CI_Controller {
  8.  
  9. protected $pre_error;
  10. private $login_param;
  11.  
  12. private $username;
  13. private $password;
  14.  
  15. // Initial function
  16. function index()
  17. {
  18. // Loading form and URL helpers
  19. $this -> load -> helper("form");
  20. $this -> load -> helper("url");
  21.  
  22.  
  23. // Loading library for sessions
  24. $this -> load -> library("session");
  25.  
  26. // Checking to see if user is already logged in
  27. // And if so, showing the logged in screen with a title and now error
  28. if($this -> session -> userdata("id"))
  29. {
  30. $this -> login_param = array(
  31. "title" => "Logged In",
  32. "error" => "You are already logged in!");
  33.  
  34. $this -> load -> view("loggedin", $this -> login_param);
  35. }
  36.  
  37. // Else, if the form was submitted, doing the login() function, and if not, refreshing the login screen
  38. else
  39. {
  40. if($_SERVER["REQUEST_METHOD"] == "POST")
  41. {
  42. $this -> login();
  43. }
  44.  
  45. else
  46. {
  47. $this -> login_param = array(
  48. "title" => "Login and Register form"
  49. );
  50.  
  51. $this -> load -> view("login", $this -> login_param);
  52. }
  53.  
  54. }
  55. }
  56.  
  57. // Login function
  58. function login()
  59. {
  60.  
  61.  
  62. // Checking to see if the form was submitted, else validating it.
  63. if($_SERVER["REQUEST_METHOD"] !== "POST")
  64. {
  65. $this -> login_param = array("title" => "Login and Register form");
  66. $this -> load -> view("login", $this -> login_param);
  67. }
  68.  
  69. else
  70. {
  71. //Loading form validation and whatnot
  72. $this -> load -> helper("form");
  73. $this -> load -> library("form_validation");
  74.  
  75. //Loading URL helper
  76. $this -> load -> helper("url");
  77.  
  78. // Setting rules for form validation
  79. $this -> form_validation -> set_rules("username", "Username", "required|min_length[2]|max_length[15]");
  80. $this -> form_validation -> set_rules("password", "Password", "required");
  81.  
  82. // Displaying errors or going to success page
  83. if($this -> form_validation -> run() == FALSE)
  84. {
  85. $this -> load -> view("login");
  86. }
  87.  
  88. else
  89. {
  90. // Connect to database
  91. $this -> load -> database();
  92.  
  93. // Securing post data
  94. $this -> load -> library("security");
  95. $this -> load -> library("encrypt");
  96.  
  97. $this -> username = $this -> input -> post("username");
  98. $this -> username = $this -> security -> xss_clean($this -> username);
  99. $this -> username = $this -> db -> escape($this -> username);
  100.  
  101.  
  102. $this -> password = $this -> input -> post("password");
  103. $this -> password = $this -> security -> xss_clean($this -> username);
  104. $this -> password = $this -> encrypt -> sha1($this -> password);
  105. $this -> password = $this -> db -> escape($this -> password);
  106.  
  107. // Querying the database for values matching the ones given
  108. $username_password_match = $this -> db -> query("SELECT * FROM `Users` WHERE `Username` = {$this -> username} && `Password` = {$this -> password}");
  109.  
  110. // If there is a match
  111. if($username_password_match -> num_rows() > 0)
  112. {
  113. $logged_in_params = array("title" => "Logged In", "error" => NULL);
  114. $this -> load -> view("loggedin", $logged_in_params);
  115. // Get ID
  116. foreach($username_password_match -> result() as $row)
  117. {
  118. $this -> session -> set_userdata("id", $row -> id);
  119. }
  120.  
  121. }
  122.  
  123. // If there isn't
  124. else
  125. {
  126. $this -> login_param = array("title" => "Login and Register forms", "login_error" => "Invalid user/password combination");
  127. $this -> load -> view("login", $this -> login_param);
  128. }
  129. }
  130. }
  131. }
  132.  
  133. function logout()
  134. {
  135. // Loading session helpers
  136. $this -> load -> library("session");
  137.  
  138. $this -> session -> unset_userdata("id");
  139. $this -> index();
  140. }
  141.  
  142. }
  143. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.