Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Logowanie PHP PDO
seba199696
post
Post #1





Grupa: Zarejestrowani
Postów: 119
Pomógł: 0
Dołączył: 4.01.2011

Ostrzeżenie: (20%)
X----


Cześć wszystkim (IMG:style_emoticons/default/smile.gif)

Napisałem logowanie do serwisu i wszystko działa poprawnie lecz proszę o wasze sugestię na temat bezpieczeństwa skryptu. Proszę o wyrozumiałość (IMG:style_emoticons/default/smile.gif) Pozdrawiam!

Daję kod:

  1. <?php
  2. ?>
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  4.  
  5. <form method="post" action="login.php">
  6. <input type="text" name="email"/>
  7. <input type="password" name="password"/>
  8. <input type="submit" name="submit"/>
  9. </form>
  10. <?php
  11. try
  12. {
  13. //conect
  14. $pdo = new PDO('mysql:host=localhost;dbname=user', 'user', '***');
  15. $pdo -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  16.  
  17. if(isset($_POST['submit'])) {
  18.  
  19. if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL))
  20. {
  21.  
  22. $password = sha1($_POST['password'].'sad8%$9sdk');
  23.  
  24. $stmt = $pdo -> prepare('SELECT `email`, `password`,`id` FROM `users` WHERE `email` = :email AND `password` = :password');
  25.  
  26. $stmt -> bindValue(':email', $_POST['email'], PDO::PARAM_STR);
  27. $stmt -> bindValue(':password', $password, PDO::PARAM_STR);
  28. $stmt -> execute();
  29.  
  30. $count = $stmt ->rowCount();
  31.  
  32. if($count > 0) {
  33.  
  34. while($row = $stmt -> fetch())
  35. {
  36.  
  37. $_SESSION['status'] = true;
  38. $_SESSION['id'] = $row['id'];
  39. $_SESSION['HTTP_USER_AGENT'] = sha1($_SERVER['HTTP_USER_AGENT'].'dsd#$%^');
  40.  
  41. header('Location: home.php');
  42. }
  43.  
  44. } else {
  45.  
  46. echo "Logowanie zakończone niepowodzeniem!";
  47.  
  48. }
  49.  
  50. } else {
  51.  
  52. echo 'Adres e-mail niepoprawny';
  53.  
  54. }
  55.  
  56. }
  57. }
  58. catch(PDOException $e)
  59. {
  60. echo 'Error: ' . $e->getMessage();
  61. }
  62. ?>
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
seba199696
post
Post #2





Grupa: Zarejestrowani
Postów: 119
Pomógł: 0
Dołączył: 4.01.2011

Ostrzeżenie: (20%)
X----


Wkleję za niedługo poprawiony skrypt (IMG:style_emoticons/default/smile.gif)

  1. <?PHP
  2. if(isset($_POST['submit'])) {
  3.  
  4. if (preg_match("/^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[!@#$%^&*()_+|-]).{8,30}$/",$_POST['password'])) {
  5.  
  6. if(preg_match('/^[a-zA-Z\.\-\_]+\@[a-zA-Z0-9\.\-\_]+\.[a-z]{2,4}$/D', $_POST['email'])) {
  7.  
  8. try
  9. {
  10. $pdo = new PDO('mysql:host=localhost;dbname=user', 'user', '***');
  11. $pdo -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  12.  
  13. $password = sha1($_POST['password'].'sad8%$9sdk');
  14.  
  15. $stmt = $pdo -> prepare('SELECT `email`, `password`,`id` FROM `users` WHERE `email` = :email AND `password` = :password');
  16.  
  17. $stmt -> bindValue(':email', $_POST['email'], PDO::PARAM_STR);
  18. $stmt -> bindValue(':password', $password, PDO::PARAM_STR);
  19. $stmt -> execute();
  20.  
  21. $count = $stmt ->rowCount();
  22.  
  23. if($count > 0) {
  24.  
  25. while($row = $stmt -> fetch())
  26. {
  27.  
  28. $_SESSION['status'] = true;
  29. $_SESSION['id'] = $row['id'];
  30. $_SESSION['HTTP_USER_AGENT'] = sha1($_SERVER['HTTP_USER_AGENT'].'dsd#$%^');
  31.  
  32. // last active the user ip
  33. $stmt = $pdo -> prepare('UPDATE `users` SET `ip` = :ip WHERE `id` = :id');
  34.  
  35. $stmt -> bindValue(':ip', $_SERVER['REMOTE_ADDR'], PDO::PARAM_STR);
  36. $stmt -> bindValue(':id', $row['id'], PDO::PARAM_STR);
  37.  
  38. $stmt -> execute();
  39. //
  40.  
  41. header('Location: index.php');
  42. }
  43.  
  44. }
  45. }
  46.  
  47. catch(PDOException $e)
  48. {
  49. echo 'Error: ' . $e->getMessage();
  50. }
  51. } else { echo "Email error"; }
  52. } else { echo "Hasło error"; }
  53. } else { echo "Formularz error"; }
  54.  
  55. ?>
  56.  


Ten post edytował seba199696 16.11.2011, 23:02:52
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: 28.12.2025 - 08:24