Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [MySQL][PHP]Logowanie w php
pawelit
post
Post #1





Grupa: Zarejestrowani
Postów: 12
Pomógł: 0
Dołączył: 14.09.2011

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


  1. <?php
  2.  
  3. class Logowanie {
  4. private $user;
  5. private $password;
  6. private $errors;
  7.  
  8. public function __construct() {
  9. $this->user = $this->filter($_POST['user']);
  10. $this->password = md5($this->filter($_POST['pass']));
  11. $this->errors = array();
  12. }
  13.  
  14. public function filter($var){
  15. return preg_replace('/[^a-zA-Z0-9@.]/','',$var);
  16. }
  17.  
  18.  
  19. public function validate(){
  20.  
  21. if(empty($this->user))
  22. $this->errors[] = 'Podaj poprawna nazwe uzytkownika';
  23. if(empty($this->password))
  24. $this->errors[] = 'Podaj poprawne haslo';
  25.  
  26. return count($this->errors)? 0 : 1;
  27.  
  28. }
  29.  
  30. public function process(){
  31. if($this->validate())
  32.  
  33. if (login() == $this->password){
  34. $_SESSION['permissions'] = 1;
  35. echo "Zalogowano";
  36. }
  37.  
  38. else { echo "kura";}
  39.  
  40. return count($this->errors)? 0 : 1;
  41. }
  42.  
  43.  
  44. public function show_errors()
  45. {
  46. echo "<h3>Errors</h3>";
  47.  
  48. foreach($this->errors as $key=>$value)
  49. echo $value."<br>";
  50. }
  51.  
  52. public function login(){
  53. try
  54.  
  55. {
  56. $pdo = new PDO('mysql:host=localhost;dbname=kom', 'root', '');
  57. $pdo -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  58.  
  59. $stmt = $pdo -> query("SELECT password FROM users WHERE user = '{$this->user}'");
  60. $stmt -> closeCursor();
  61. }
  62.  
  63. catch(PDOException $e)
  64.  
  65. {
  66. echo 'Połączenie nie mogło zostać utworzone: ' . $e->getMessage();
  67. }
  68. return $stmt;
  69. }
  70. ?>



  1. <html>
  2. <head>
  3. <title>
  4. logowanie
  5. </title>
  6. </head>
  7. <body>
  8.  
  9.  
  10.  
  11. <?php
  12. if (isset ($_POST['login']))
  13. {
  14. include_once '/logowanie.php';
  15. $a = new Logowanie();
  16. if($a->process())
  17. echo "Login!";
  18. else
  19. $a->show_errors();
  20.  
  21. }
  22.  
  23. ?>
  24.  
  25.  
  26.  
  27. <form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
  28. <table>
  29. <tr><td>User:</td><td><input type="text" name="user"/></td></tr>
  30. <tr><td>Password:</td><td><input type="password" name="pass"/></td></tr>
  31. </table>
  32.  
  33. <input type="submit" name="login" value="Log in"/>
  34. </form>
  35.  
  36.  
  37. </body>
  38. </html>



Niestety nie loguje. Czy taka konstrukcja jest prawidłowa:
  1. if (login() == $this->password){
  2. $_SESSION['permissions'] = 1;
  3. echo "Zalogowano";
  4. }
?

Ten post edytował pawelit 14.09.2011, 12:58:04
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: 25.08.2025 - 06:48