Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [MySQL][PHP]Logowanie 2-etapowe (projekt)
gloweres
post
Post #1





Grupa: Zarejestrowani
Postów: 20
Pomógł: 0
Dołączył: 29.08.2020

Ostrzeżenie: (0%)
-----


Hej, zaprojektowałem proste logowanie 2-etapowe (na maila przychodzi kod do logowania po wpisaniu hasła).
Logowanie to jest używane tylko przez 2 osoby ale do bardzo wrażliwych danych więc mam następujące pytanie: czy jest bezpieczne? Może popełniłem jakiś błąd który może powodować "włam"? Dzięki z góry za odpowiedź (IMG:style_emoticons/default/smile.gif)


login.php
  1. <?php
  2.  
  3.  
  4. require 'dbsoft.php';
  5.  
  6. $password = $_POST['password'];
  7.  
  8. $email = htmlentities($_POST['email'], ENT_QUOTES, "UTF-8");
  9.  
  10. $sekret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
  11.  
  12. $sprawdz = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$sekret.'&response='.$_POST['g-recaptcha-response']);
  13.  
  14. $odpowiedz = json_decode($sprawdz);
  15.  
  16. if ($odpowiedz->success==false)
  17. {
  18. $_SESSION['e_bot']="Potwierdź, że nie jesteś botem!";
  19. header('Location: ../login.php');
  20. exit();
  21. }
  22. $sql = 'SELECT * FROM account WHERE email=?';
  23. $statement = $connection->prepare($sql);
  24. $statement->execute([$email]);
  25. $oz = $statement->rowCount();
  26.  
  27. if($oz>0){
  28.  
  29. $row = $statement->fetchAll(PDO::FETCH_OBJ);
  30.  
  31. foreach($row as $rows):
  32. $password_base = $rows->password;
  33. $email = $rows->email;
  34. $id_base = $rows->id;
  35. endforeach;
  36.  
  37. if (password_verify($password, $password_base))
  38. {
  39.  
  40. $_SESSION['verify'] = true;
  41. $_SESSION['id_session'] = $id_base;
  42.  
  43. function random(int $i)
  44. {
  45. return substr(str_shuffle(str_repeat($x='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil($i/strlen($x)) )),1,$i);
  46. }
  47.  
  48. $code = random(35);
  49. $now = time() + 900;
  50. $time = date("Y-m-d H:i:s");
  51.  
  52. $sql = "INSERT INTO verify_code (id_user, code, date, status_code) VALUES (?, ?, ?, ?)";
  53. $connection->prepare($sql)->execute([$id_base, $code, $now, 0]);
  54.  
  55. $message="Kod do weryfikacji:\n\n$code\n\nWiadomosc zostala wygenerowana automatycznie: $time";
  56. $sender="From: domena.pl";
  57. @mail($email, "Kod weryfikacyjny", $message, $sender);
  58.  
  59. unset($_SESSION['error']);
  60.  
  61. $statement = null;
  62. $connection = null;
  63.  
  64. header('Location: verify.php');
  65.  
  66. }
  67. else
  68. {
  69.  
  70. $_SESSION['error'] = '<span style="color:red">Nieprawidłowy login lub hasło!</span>';
  71. $statement = null;
  72. $connection = null;
  73. header('Location: ../login.php');
  74.  
  75. }
  76.  
  77. } else {
  78.  
  79. $_SESSION['error'] = '<span style="color:red">Nieprawidłowy login lub hasło!</span>';
  80. $statement = null;
  81. $connection = null;
  82. header('Location: ../login.php');
  83.  
  84. }
  85.  
  86. $statement = null;
  87. $connection = null;
  88.  
  89. ?>



verify_check.php:
  1. <?php
  2.  
  3.  
  4. if (!isset($_SESSION['verify']))
  5. {
  6. header('Location: ../login.php');
  7. exit();
  8. }
  9.  
  10.  
  11. if(empty($_POST["code"]) || !isset($_POST["code"])){
  12.  
  13. $_SESSION['error'] = '<span style="color:red">Nie podano kodu</span>';
  14. header('Location: verify.php');
  15.  
  16. }
  17.  
  18. $code = htmlentities($_POST["code"], ENT_QUOTES, "UTF-8");
  19.  
  20. require '../database_connect/dbsoft.php';
  21.  
  22. $sql = 'SELECT * FROM verify_code WHERE code=?';
  23. $statement = $connection->prepare($sql);
  24. $statement->execute([$code]);
  25.  
  26. $row = $statement->fetchAll(PDO::FETCH_OBJ);
  27.  
  28. foreach($row as $rows):
  29. $id_user = $rows->id_user;
  30. $date = $rows->date;
  31. $code_base = $rows->code;
  32. $status_base = $rows->status_code;
  33. $ide = $rows->id;
  34. endforeach;
  35.  
  36. $now = time();
  37.  
  38. if($status_base == 0){
  39. if($code_base == $code){
  40.  
  41. if($_SESSION['id_session'] == $id_user){
  42.  
  43. if($now <= $date){
  44.  
  45. $_SESSION['admin'] = true;
  46.  
  47. $st = 1;
  48.  
  49. $sql = "UPDATE verify_code SET status_code=? WHERE id=?";
  50. $statement = $connection->prepare($sql);
  51. $statement->execute([$st, $ide]);
  52.  
  53. unset($_SESSION['verify']);
  54.  
  55. $statement = null;
  56. $connection = null;
  57.  
  58. header('Location: ../../');
  59. exit();
  60.  
  61. }else{
  62. echo 'Czas minał';
  63. }
  64.  
  65. }else{
  66. echo 'To nie ty!';
  67. }
  68.  
  69. }else{
  70. echo 'Nieprawidłowy kod';
  71. }
  72. }else{
  73. echo 'Kod został zużyty';
  74. }


Jeśli jest dobrze to proszę też o odpowiedź (IMG:style_emoticons/default/smile.gif)

Ten post edytował gloweres 9.11.2020, 03:00:50
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 2.04.2026 - 10:08