123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- <?php namespace App\Controllers;
- use CodeIgniter\Controller;
- use \App\Models\UserModel;
- use CodeIgniter\I18n\Time;
- class Login extends BaseController
- {
- protected $regex= '/[^A-Za-z0-9!?\s]+/';
- //above should clean everything except numbers, letters white space
- protected $name;
- protected $password;
- protected $captchaEntered;
- protected $actualCaptcha;
- protected $passwordMatches;
- protected $hashPassword;
- protected $userNameDB;
- protected $userNameMatches;
- protected $captchaMatches;
- protected $myTime;
- protected $myDate;
- public function __construct()
- {
- parent::__construct();
- $this->myTime = parent::getTime();
- $this->myDate= date("d/m/Y",$this->myTime);
-
- }
-
- public function index()
- {
- echo "heelo from login";
- }
- public function login()
- {
- session_start();
- $myarray = array( chr(rand(48,57)) , chr(rand(48,57)) , chr(rand(48,57)) , chr(rand(48,57)) , chr(rand(48,57)));
- $_SESSION['captcha']= $myarray;
- $data = [
- 'title' => 'login page',
- 'captcha'=>$myarray,
- 'date'=>$this->myDate
-
- ];
-
- echo view('usrLogin',$data);
-
-
-
-
- }
- public function credentials()
- {
- session_start();
- //maybe should use removeallbad chars here
- $theArray= $_SESSION['captcha'];
- $this->actualCaptcha= implode("",$theArray);
- $name = $this->request->getVar('user');
- $name2 = htmlentities($name,ENT_QUOTES);
- $this->name= preg_replace($this->regex,"",$name2);
- $this->name= trim($this->name);
-
- $password= $this->request->getVar('userPassword');
- $password2= htmlentities($password,ENT_QUOTES);
- $this->password= preg_replace($this->regex,"",$password2);
- $this->password= trim($this->password);
-
- $captcha= $this->request->getVar('captcha');
- $captcha2= htmlentities($captcha,ENT_QUOTES);
- $this->captcha= preg_replace($this->regex,"",$captcha2);
- $this->captcha= trim($this->captcha);
- $handle= new UserModel();
- try {
- $result2= $handle->getOne('admin');
- $this->hashPassword= $result2['Password'];
- $this->userNameDB=$result2['Name'];
- }
- catch (\Exception $e)
- {
- echo ($e->getMessage());
- //can do redirect here etc
- $data= [
-
- 'title'=>'problem',
-
- 'info'=>$e->getMessage(),
- 'date'=>$this->myDate
-
- ];
-
-
- echo view('info', $data);
-
-
-
-
-
- }
- $this->passwordMatches= password_verify($this->password,$this->hashPassword);
- $this->userNameMatches= strcmp($this->userNameDB, $this->name);
- //if true value will be 0
- $this->captchaMatches=strcmp($this->captcha,$this->actualCaptcha);
- if ( ($this->userNameMatches ==0) && ($this->captchaMatches ==0) &&($this->passwordMatches == True) )
- {
-
- $_SESSION['role']="admin";
-
- $data=
-
-
- [
- 'title'=>'info',
- 'info'=>'<p>
-
- <h11>Hello</h11> admin your now logged in
- </p> ' ,
- 'date'=>$this->myDate
-
-
- ];
-
-
- echo view('admin', $data);
-
-
-
- }
- else
- {
-
- $logic =isset( $_SESSION['count']);
- if ($logic ==false)
- {
- $_SESSION['count']=5;
- return redirect()->route('orange');
-
-
-
- }
-
- elseif($logic==true)
-
- {
- if( $_SESSION['count']<=1 )
- {
-
- return redirect()->to('http://www.google.com');
-
- }
-
- else
-
- {
-
- $oldCount= $_SESSION['count'];
- $newCount = $oldCount-1;
- $_SESSION['count']= $newCount;
-
-
-
-
- return redirect()->route('orange');
-
-
- }
-
-
-
-
- }
-
-
- }
- }
- public function logout()
- {
- session_start();
-
-
-
-
- unset($_SESSION['role']);
- unset($_SESSION['count']);
-
- $data= [
- 'title'=> 'logout',
- 'info'=> 'you may have already been logged out, but if you were not you are now !' ,
- 'date'=>$this->myDate
-
-
-
- ];
-
-
-
- echo view('info', $data);
-
-
-
-
-
-
-
-
-
- }
- public function admin()
- {
-
-
-
-
-
- $data= [
- 'title'=> 'admin',
- 'info'=> 'your logged in dont forget to logout' ,
- 'date'=>$this->myDate
-
-
-
- ];
-
-
-
- echo view('admin', $data);
-
- }
- }
-
|