Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [MySQL][PHP] Wielkość liter- problem
Forum PHP.pl > Forum > Przedszkole
mentoos
W swojej klasie logowania mam problem z rozpoznawaniem wielkości liter. Kiedy próbuję się zalogować, pisząc nick z dużej litery, to jest ok loguje, ale jeśli z małej. Problem jest w tym, że kodowanie bazy jest ustawione na utf8_general_ci , więc nie powinno rozróżniać wielkości liter. Moduł Auth z frameworka Kohany, którego uzywam, ma to samo kodowanie i tam loguje normalnie jeśli wpiszę nick z małej litery.

Kod klasy
  1. <?php
  2.  
  3. class Auth
  4. {
  5. private $username;
  6. private $password;
  7. private $salt;
  8. private $pdo;
  9. public $message;
  10.  
  11. public function __construct()
  12. {
  13. require_once ('config.php');
  14. $this-> pdo= new PDO ($config['dsn'], $config['user'], $config['password']);
  15. }
  16.  
  17. public function get_data()
  18. {
  19. $start= microtime();
  20. $stmt= $this->pdo->prepare ('SELECT username,password,salt FROM users WHERE username=:username ORDER BY id');
  21. $stmt-> bindValue (':username', $_POST['username'], PDO::PARAM_STR);
  22. $stmt-> execute();
  23. $end= microtime();
  24. while ($row= $stmt-> fetch())
  25. {
  26. $this-> username= $row['username'];
  27. $this-> password= $row['password'];
  28. $this-> salt= $row['salt'];
  29. }
  30. $stmt-> closeCursor();
  31. $this-> result= substr ($end - $start, 0 , 6);
  32. }
  33.  
  34. public function login($username, $password)
  35. {
  36. if ($this-> username == $username && $this-> password == $this->code($password, $this->salt))
  37. {
  38. $_SESSION['user']= TRUE;
  39. $this-> message= 'Welcome , ' . '<a href="auth.php?signout=true">Sign out</a>';
  40. }
  41. else
  42. {
  43. $this-> message= 'Invalid username/password';
  44. }
  45. }
  46.  
  47. public function signout()
  48. {
  49. if ($_GET['signout'] == TRUE)
  50. {
  51. header ("Location: login.php");
  52. }
  53. }
  54.  
  55. public function signup($username, $password, $email, $salt)
  56. {
  57. if (! empty($username) && ! empty($password) && ! empty($email))
  58. {
  59. if ($this-> valid_email($_POST['email']) && $this->email_exists($_POST['email']))
  60. {
  61. $stmt= $this->pdo->prepare ('INSERT INTO users (username,password,email,salt)
  62. VALUES (:username,:password,:email,:salt) ');
  63. $stmt-> bindvalue (':username', htmlspecialchars($username),PDO::PARAM_STR);
  64. $stmt-> bindvalue (':password', htmlspecialchars($password),PDO::PARAM_STR);
  65. $stmt-> bindvalue (':email', htmlspecialchars($email),PDO::PARAM_STR);
  66. $stmt-> bindvalue (':salt', $salt,PDO::PARAM_STR);
  67. $stmt-> execute();
  68. $this-> message= 'Account has been created.';
  69. $stmt-> closeCursor();
  70. }
  71. }
  72. else
  73. {
  74. $this-> message= 'Fill in all fields.';
  75. }
  76. }
  77.  
  78. private function email_exists($email)
  79. {
  80. $stmt= $this->pdo->prepare ('SELECT email FROM users WHERE email=:email');
  81. $stmt-> bindValue (':email', $_POST['email'], PDO::PARAM_STR);
  82. $stmt-> execute();
  83.  
  84. if ($stmt-> fetch() > 0)
  85. {
  86. $this-> message= 'This email address exists.';
  87. }
  88. else
  89. {
  90. return true;
  91. }
  92. }
  93.  
  94. private function valid_email($email)
  95. {
  96. if (preg_match('/^[a-zA-Z0-9\.\-\_]+\@[a-zA-Z0-9\.\-\_]+\.[a-z]{2,4}$/D', $email))
  97. {
  98. return true;
  99. }
  100. else
  101. {
  102. $this-> message= 'E-mail is invalid.';
  103. }
  104. }
  105.  
  106. public function salt($pass)
  107. {
  108. return substr(md5(microtime() . $pass), 0, 6);
  109. }
  110.  
  111. public function code($pass, $salt)
  112. {
  113. return md5 ($pass . $salt);
  114. }
  115.  
  116. public function show()
  117. {
  118. return $this-> message;
  119. }
  120. }
  121.  
  122. $user= new Auth;
  123. $user-> get_data();
  124. $user-> login($_POST['username'],$_POST['password']);
  125. $user-> signout();
  126. echo $user-> show();
  127. echo $user-> result;
  128.  
  129. ?>
franki01
utf8_general_ci nie rozróżnia wielkości liter. Spróbuj z utf8_bin.
mentoos
To też nie pomogło. Próbowałem odpalić na WAMP , bo używam XAMPP , ale dzieje się to samo. Moduł Auth z framework Kohana normalnie loguje jak napiszę nick z małej litery. Czy jest to wina mojeje klasy czy czegoś innego?
franki01
Jak mogło nie pomóc, skoro utf8_bin sam w sobie jest binarny i ważna jest wielkość liter. Zmień metodę porównywania potrzebnej kolumny na utf8_bin i nie ma mocnych, żeby nie działało.
mentoos
Nie wiem właśnie, dlaczego nie działa. utf8_bin jest ustawiony na wszystkie kolumny i dalej jak próbuję się zalogować z nicka z małej litery to nie działa. Napisałem inny skrypt specjalnie, żeby sprawdzić i jest to samo. Też nie loguje mnie jeśli nick w bazie jest z dużej litery,a ten z formularza jest pisany z małej.

Dziwne jest to, że dzieje się też odwrotnie. Jak nick w bazie jest z małej litery, a w formularzu loguję się z dużej, to też nie loguje.
jinks
Kiedyś miałem podobny problem - zmiana metody porównywania zaczęła poprawnie funkcjonować dopiero po truncate tabeli (chyba wtedy ją przeładowywuje). Metoda czołgowa, ale czasem warto sprawdzić smile.gif.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.