Login.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php namespace App\Controllers;
  2. use CodeIgniter\Controller;
  3. use \App\Models\UserModel;
  4. use CodeIgniter\I18n\Time;
  5. class Login extends BaseController
  6. {
  7. protected $regex= '/[^A-Za-z0-9!?\s]+/';
  8. //above should clean everything except numbers, letters white space
  9. protected $name;
  10. protected $password;
  11. protected $captchaEntered;
  12. protected $actualCaptcha;
  13. protected $passwordMatches;
  14. protected $hashPassword;
  15. protected $userNameDB;
  16. protected $userNameMatches;
  17. protected $captchaMatches;
  18. protected $myTime;
  19. protected $myDate;
  20. public function __construct()
  21. {
  22. parent::__construct();
  23. $this->myTime = parent::getTime();
  24. $this->myDate= date("d/m/Y",$this->myTime);
  25. }
  26. public function index()
  27. {
  28. echo "heelo from login";
  29. }
  30. public function login()
  31. {
  32. session_start();
  33. $myarray = array( chr(rand(48,57)) , chr(rand(48,57)) , chr(rand(48,57)) , chr(rand(48,57)) , chr(rand(48,57)));
  34. $_SESSION['captcha']= $myarray;
  35. $data = [
  36. 'title' => 'login page',
  37. 'captcha'=>$myarray,
  38. 'date'=>$this->myDate
  39. ];
  40. echo view('usrLogin',$data);
  41. }
  42. public function credentials()
  43. {
  44. session_start();
  45. //maybe should use removeallbad chars here
  46. $theArray= $_SESSION['captcha'];
  47. $this->actualCaptcha= implode("",$theArray);
  48. $name = $this->request->getVar('user');
  49. $name2 = htmlentities($name,ENT_QUOTES);
  50. $this->name= preg_replace($this->regex,"",$name2);
  51. $this->name= trim($this->name);
  52. $password= $this->request->getVar('userPassword');
  53. $password2= htmlentities($password,ENT_QUOTES);
  54. $this->password= preg_replace($this->regex,"",$password2);
  55. $this->password= trim($this->password);
  56. $captcha= $this->request->getVar('captcha');
  57. $captcha2= htmlentities($captcha,ENT_QUOTES);
  58. $this->captcha= preg_replace($this->regex,"",$captcha2);
  59. $this->captcha= trim($this->captcha);
  60. $handle= new UserModel();
  61. try {
  62. $result2= $handle->getOne('admin');
  63. $this->hashPassword= $result2['Password'];
  64. $this->userNameDB=$result2['Name'];
  65. }
  66. catch (\Exception $e)
  67. {
  68. echo ($e->getMessage());
  69. //can do redirect here etc
  70. $data= [
  71. 'title'=>'problem',
  72. 'info'=>$e->getMessage(),
  73. 'date'=>$this->myDate
  74. ];
  75. echo view('info', $data);
  76. }
  77. $this->passwordMatches= password_verify($this->password,$this->hashPassword);
  78. $this->userNameMatches= strcmp($this->userNameDB, $this->name);
  79. //if true value will be 0
  80. $this->captchaMatches=strcmp($this->captcha,$this->actualCaptcha);
  81. if ( ($this->userNameMatches ==0) && ($this->captchaMatches ==0) &&($this->passwordMatches == True) )
  82. {
  83. $_SESSION['role']="admin";
  84. $data=
  85. [
  86. 'title'=>'info',
  87. 'info'=>'<p>
  88. <h11>Hello</h11> admin your now logged in
  89. </p> ' ,
  90. 'date'=>$this->myDate
  91. ];
  92. echo view('admin', $data);
  93. }
  94. else
  95. {
  96. $logic =isset( $_SESSION['count']);
  97. if ($logic ==false)
  98. {
  99. $_SESSION['count']=5;
  100. return redirect()->route('orange');
  101. }
  102. elseif($logic==true)
  103. {
  104. if( $_SESSION['count']<=1 )
  105. {
  106. return redirect()->to('http://www.google.com');
  107. }
  108. else
  109. {
  110. $oldCount= $_SESSION['count'];
  111. $newCount = $oldCount-1;
  112. $_SESSION['count']= $newCount;
  113. return redirect()->route('orange');
  114. }
  115. }
  116. }
  117. }
  118. public function logout()
  119. {
  120. session_start();
  121. unset($_SESSION['role']);
  122. unset($_SESSION['count']);
  123. $data= [
  124. 'title'=> 'logout',
  125. 'info'=> 'you may have already been logged out, but if you were not you are now !' ,
  126. 'date'=>$this->myDate
  127. ];
  128. echo view('info', $data);
  129. }
  130. public function admin()
  131. {
  132. $data= [
  133. 'title'=> 'admin',
  134. 'info'=> 'your logged in dont forget to logout' ,
  135. 'date'=>$this->myDate
  136. ];
  137. echo view('admin', $data);
  138. }
  139. }