Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.87 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", "trim|required");
  27. $this->form_validation->set_rules("txt_password", "Password", "trim|required");
  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, $password);
  41. if ($usr_result > 0) //active user record is present
  42. {
  43. //set the session variables
  44. $sessiondata = array(
  45. 'username' => $username,
  46. 'loginuser' => TRUE
  47. );
  48. $this->session->set_userdata($sessiondata);
  49. redirect("index");
  50. }
  51. else
  52. {
  53. $this->session->set_flashdata('msg', '<div class="alert alert-danger text-center">Invalid username and password!</div>');
  54. redirect('login/index');
  55. }
  56. }
  57. else
  58. {
  59. redirect('login/index');
  60. }
  61. }
  62. }
  63. }?>
  64.  
  65. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  66.  
  67. class login_model extends CI_Model
  68. {
  69. function __construct()
  70. {
  71. // Call the Model constructor
  72. parent::__construct();
  73. }
  74.  
  75. //get the username & password from tbl_usrs
  76. function get_user($usr, $pwd)
  77. {
  78. $sql = "select * from punetor where username = '" . $usr . "' and password = '" . md5($pwd) . "' and status = 'active'";
  79. $query = $this->db->query($sql);
  80. return $query->num_rows();
  81. }
  82. }?>
  83.  
  84. <<!DOCTYPE html>
  85. <html>
  86. <head>
  87. <meta charset="utf-8">
  88. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  89. <title>Login Form</title>
  90. <!--link the bootstrap css file-->
  91. <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
  92.  
  93. <style type="text/css">
  94. .colbox {
  95. margin-left: 0px;
  96. margin-right: 0px;
  97. }
  98. </style>
  99. </head>
  100. <body>
  101. <div class="container">
  102. <div class="row">
  103. <div class="col-lg-6 col-sm-6">
  104. <h1>LIVEDOTCOM</h1>
  105. </div>
  106. <div class="col-lg-6 col-sm-6">
  107.  
  108. <ul class="nav nav-pills pull-right" style="margin-top:20px">
  109. <li class="active"><a href="#">Login</a></li>
  110. <li><a href="#">Signup</a></li>
  111. </ul>
  112.  
  113. </div>
  114. </div>
  115. </div>
  116. <hr/>
  117.  
  118. <div class="container">
  119. <div class="row">
  120. <div class="col-lg-4 col-sm-4 well">
  121. <?php
  122. $attributes = array("class" => "form-horizontal", "id" => "loginform", "name" => "loginform");
  123. echo form_open("login/index", $attributes);?>
  124. <fieldset>
  125. <legend>Login</legend>
  126. <div class="form-group">
  127. <div class="row colbox">
  128. <div class="col-lg-4 col-sm-4">
  129. <label for="txt_username" class="control-label">Username</label>
  130. </div>
  131. <div class="col-lg-8 col-sm-8">
  132. <input class="form-control" id="txt_username" name="txt_username" placeholder="Username" type="text" value="<?php echo set_value('txt_username'); ?>" />
  133. <span class="text-danger"><?php echo form_error('txt_username'); ?></span>
  134. </div>
  135. </div>
  136. </div>
  137.  
  138. <div class="form-group">
  139. <div class="row colbox">
  140. <div class="col-lg-4 col-sm-4">
  141. <label for="txt_password" class="control-label">Password</label>
  142. </div>
  143. <div class="col-lg-8 col-sm-8">
  144. <input class="form-control" id="txt_password" name="txt_password" placeholder="Password" type="password" value="<?php echo set_value('txt_password'); ?>" />
  145. <span class="text-danger"><?php echo form_error('txt_password'); ?></span>
  146. </div>
  147. </div>
  148. </div>
  149.  
  150. <div class="form-group">
  151. <div class="col-lg-12 col-sm-12 text-center">
  152. <input id="btn_login" name="btn_login" type="submit" class="btn btn-default" value="Login" />
  153. <input id="btn_cancel" name="btn_cancel" type="reset" class="btn btn-default" value="Cancel" />
  154. </div>
  155. </div>
  156. </fieldset>
  157. <?php echo form_close(); ?>
  158. <?php echo $this->session->flashdata('msg'); ?>
  159. </div>
  160. </div>
  161. </div>
  162.  
  163. <!--load jQuery library-->
  164. <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  165. <!--load bootstrap.js-->
  166. <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
  167. </body>
  168. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement