Advertisement
themate0

Untitled

Jan 18th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. <?php
  2. session_start();
  3. require_once("class.DB.php");
  4.  
  5. class Security {
  6. public $message = "";
  7. public function __construct(){}
  8.  
  9. public function register(){
  10. if($_SERVER['REQUEST_METHOD'] === 'POST'){
  11. if(isset($_POST['login']) && isset($_POST['pass']) && isset($_POST['repeat'])){
  12. $login = htmlspecialchars($_POST['login']);
  13. $pass = $_POST['pass'];
  14. $repeat = $_POST['repeat'];
  15.  
  16. $db = new DB();
  17. $t = $db->query("select * from users where username=\"$login\"");
  18.  
  19. if(count($t) > 0){
  20. $this->message = "Twój login jest zajęty!";
  21. return;
  22. }
  23.  
  24. if($pass != $repeat){
  25. $this->message = "Hasła muszą być takie same!";
  26. return;
  27. }
  28.  
  29. if(strlen($login) < 4){
  30. $this->message = "Login musi zawierac przynajmniej 4 znaki";
  31. return;
  32. }
  33.  
  34. if(strlen($pass) > 4){
  35. $db->myquery("insert into users(username, password) values(\"$login\", \"$pass\")");
  36. $this->message = "Użytkownik $login zostal utworzony.<br>Za chwile zostaniesz przekierowany do logowania!";
  37. header("refresh:2, /login.php");
  38.  
  39. }else{
  40. $this->message = "Hasło musi składać się z przynajmniej 5 znaków!";
  41. return;
  42. }
  43.  
  44.  
  45.  
  46. }
  47. }
  48. }
  49.  
  50. public function login(){
  51. if($_SERVER['REQUEST_METHOD'] === 'POST'){
  52. if(isset($_POST['username']) && isset($_POST['password'])){
  53. $username = $_POST['username'];
  54. $password = $_POST['password'];
  55.  
  56. $db = new DB();
  57. $t = $db->query("select * from users where username=\"$username\"");
  58. if(count($t) == 1){
  59. if($t[0]['password'] == $password){
  60. $_SESSION['username'] = $username;
  61. $this->message = "Zalogowales się poprawnie";
  62. }else{
  63. $this->message = "Zły login lub hasło";
  64. }
  65. }else{
  66. $this->message = "Zły login lub hasło";
  67. }
  68. }
  69. }
  70. }
  71.  
  72. public function logout(){
  73. unset($_SESSION['username']);
  74. }
  75.  
  76. public function isLogin(){
  77. return isset($_SESSION['username']);
  78. }
  79.  
  80. public function checkLogin(){
  81. if(!$this->isLogin()){
  82. header("Location: login.php");
  83. die();
  84. }
  85. }
  86.  
  87.  
  88. }
  89.  
  90.  
  91.  
  92. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement