Advertisement
Guest User

Untitled

a guest
Dec 10th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.70 KB | None | 0 0
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2.  
  3. class login extends CI_Controller
  4. {
  5.  
  6. public function __construct()
  7. {
  8. parent::__construct();
  9. $this->load->library('session');
  10. $this->load->helper('form');
  11. $this->load->helper('url');
  12. $this->load->helper('html');
  13. $this->load->database();
  14. $this->load->library('form_validation');
  15. //load the login model
  16. $this->load->model('login_model');
  17. }
  18.  
  19. public function index()
  20. {
  21. //get the posted values
  22. $username = $this->input->post("txt_username");
  23. $password = $this->input->post("txt_password");
  24.  
  25. //set validations
  26. $this->form_validation->set_rules("txt_username", "Username");
  27. $this->form_validation->set_rules("txt_password", "Password");
  28.  
  29. if ($this->form_validation->run() == FALSE)
  30. {
  31. //validation fails
  32. $this->load->view('login_view');
  33. }
  34. else
  35. {
  36. //validation succeeds
  37. if ($this->input->post('btn_login') == "Login")
  38. {
  39. //check if username and password is correct
  40. $usr_result = $this->login_model->get_user($username,
  41. $password);
  42. if ($usr_result > 0) //active user record is present
  43. {
  44. //set the session variables
  45. $sessiondata = array(
  46. 'username' => $username,
  47. 'loginuser' => TRUE
  48. );
  49. $this->session->set_userdata($sessiondata);
  50. redirect("index");
  51. }
  52. else
  53. {
  54. $this->session->set_flashdata('msg', '<div class="alert
  55.  
  56. alert-danger text-center">Invalid username and password!</div>');
  57. redirect('login/index');
  58. }
  59. }
  60. else
  61. {
  62. redirect('login/index');
  63. }
  64. }
  65. }
  66. }?>
  67.  
  68. My model: login_model.php
  69.  
  70.  
  71. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  72.  
  73. class login_model extends CI_Model
  74. {
  75. function __construct()
  76. {
  77. // Call the Model constructor
  78. parent::__construct();
  79. }
  80.  
  81. //get the username & password from tbl_usrs
  82. function get_user($usr, $pwd)
  83. {
  84. $sql = "select * from tbl_users where username = '" . $usr . "' and
  85. password = '" . md5($pwd) . "' and status = 'active'";
  86. $query = $this->db->query($sql);
  87. //echo $sql; exit;
  88. return $query->num_rows();
  89.  
  90. }
  91.  
  92. }?>
  93. My view: login_view.php
  94.  
  95. <!DOCTYPE html>
  96. <html>
  97. <head>
  98. <meta charset="utf-8">
  99. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  100. <title>Login Form</title>
  101. <!--link the bootstrap css file-->
  102. <link
  103.  
  104. href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"
  105. rel="stylesheet">
  106.  
  107. <style type="text/css">
  108. .colbox {
  109. margin-left: 0px;
  110. margin-right: 0px;
  111. }
  112. </style>
  113. </head>
  114. <body>
  115. <div class="container" style="margin-top: 100px;">
  116. <div class="row">
  117. <div class="col-lg-6 col-sm-6">
  118. <h1>Welcome!</h1>
  119. </div>
  120. <div class="col-lg-6 col-sm-6">
  121. <ul class="nav nav-pills pull-right" style="margin-top:20px">
  122. <li class="active"><a href="#">Login</a></li>
  123.  
  124. </ul>
  125. </div>
  126. </div>
  127. </div>
  128. <hr/>
  129.  
  130. <div class="container">
  131. <div class="row">
  132. <div class="col-lg-4 col-sm-4 well">
  133. <?php
  134. $attributes = array("class" => "form-horizontal", "id" => "loginform",
  135.  
  136. "name" => "loginform");
  137. echo form_open("login/index", $attributes);?>
  138. <fieldset>
  139. <legend>Login</legend>
  140. <div class="form-group">
  141. <div class="row colbox">
  142. <div class="col-lg-4 col-sm-4">
  143. <label for="txt_username" class="control-
  144. label">Username</label>
  145. </div>
  146. <div class="col-lg-8 col-sm-8">
  147. <input class="form-control" id="txt_username"
  148. name="txt_username" placeholder="Username" type="text" value="<?php echo
  149. set_value('txt_username'); ?>" />
  150. <span class="text-danger"><?php echo
  151. -form_error('txt_username'); ?></span>
  152. </div>
  153. </div>
  154. </div>
  155.  
  156. <div class="form-group">
  157. <div class="row colbox">
  158. <div class="col-lg-4 col-sm-4">
  159. <label for="txt_password" class="control-label">Password</label>
  160. </div>
  161. <div class="col-lg-8 col-sm-8">
  162. <input class="form-control" id="txt_password"
  163. name="txt_password" placeholder="Password" type="password" value="<?php
  164. echo set_value('txt_password'); ?>" />
  165. <span class="text-danger"><?php echo
  166. form_error('txt_password'); ?></span>
  167. </div>
  168. </div>
  169. </div>
  170.  
  171. <div class="form-group">
  172. <div class="col-lg-12 col-sm-12 text-center">
  173. <input id="btn_login" name="btn_login" type="submit"
  174. class="btn btn-default" value="Login" />
  175. <input id="btn_cancel" name="btn_cancel" type="reset"
  176. class="btn btn-default" value="Cancel" />
  177. </div>
  178. </div>
  179. </fieldset>
  180. <?php echo form_close(); ?>
  181. <?php echo $this->session->flashdata('msg'); ?>
  182. </div>
  183. </div>
  184. </div>
  185.  
  186. <!--load jQuery library-->
  187. <script
  188.  
  189. src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
  190. </script>
  191. <!--load bootstrap.js-->
  192. <script
  193. src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js">
  194.  
  195. </script>
  196. </body>
  197. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement