Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [MySQL][PHP] Logowanie i brak polskich znaków.
Mlody993
post 8.03.2016, 20:41:23
Post #1





Grupa: Zarejestrowani
Postów: 81
Pomógł: 0
Dołączył: 12.02.2009

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


Witam,
mam problem z kodowaniem. Udało mi się ustawić komentarze, rejestracje - tutaj kodowanie UTF-8 działa, wszystko jest w porządku.

Problem pojawia się jednak przy logowaniu. Jeżeli utworzę poprzez formularz rejestracyjny użytkownika o nazwie użytkownik, to w bazie istnieje "użytkownik". Natomiast formularz logowania niestety nie rozpoznaje wpisanych polskich znaków. Może ktoś będzie w stanie mi pomóc.


Tutaj jest plik login.php:
  1. <p class="ng">Logowanie</p>
  2. <p>Zaloguj się podając swój login i hasło.<br><br></p>
  3.  
  4.  
  5. <?php
  6.  
  7.  
  8. if ((isset($_SESSION['zalogowany'])) && ($_SESSION['zalogowany']==true))
  9. {
  10. header('Location: /panel,pl.html');
  11. exit();
  12. }
  13.  
  14. ?>
  15. <?php
  16. if(isset($_SESSION['blad'])) echo $_SESSION['blad'];
  17. ?>
  18. <form action="/zaloguj,pl.html" method="post">
  19. <table border="2" style="border-style: none;" width="200" align="center">
  20. <tr>
  21. <th style="border-color: #6495ED; background-color: #FAF0E6; -moz-border-radius: 6px; border-style: solid;" align="center">
  22. Login:</th><th style="border-color: #6495ED; background-color: #FAF0E6; -moz-border-radius: 6px; border-style: solid;" align="center">
  23. <input type="text" name="login" style="color:#39759B; font-weight: bold;" /></th>
  24. </tr>
  25. <tr>
  26. <th style="border-color: #6495ED; background-color: #FAF0E6; -moz-border-radius: 6px; border-style: solid;" align="center">
  27. Hasło:</th>
  28. <th style="border-color: #6495ED; background-color: #FAF0E6; -moz-border-radius: 6px; border-style: solid;" align="center">
  29. <input type="password" name="pass" style="color:#39759B; font-weight: bold;" /></th>
  30. </tr>
  31. </table>
  32. <table border="1" style="border-style: none;" width="20" align="center">
  33. <tr>
  34. <th style="border-color: #6495ED; background-color: #FAF0E6; -moz-border-radius: 6px; border-style: solid;" width="200" align="center">
  35. <input type="submit" value="Zaloguj" /></th>
  36.  
  37. </form>
  38. </th>
  39. </tr>
  40. </table>


A tutaj plik "zaloguj.php" gdzie jest wszystko dotyczące sesji i logowania:
  1. <?php
  2.  
  3.  
  4. class user {
  5.  
  6. public function passSalter ($pass) {
  7. $pass = '$@@#$#@$'.$pass.'q2#$3$%##@';
  8. return md5($pass);
  9. }
  10.  
  11. }
  12.  
  13. if ((!isset($_POST['login'])) || (!isset($_POST['pass'])))
  14. {
  15. header('Location: /login,pl.html');
  16. exit();
  17.  
  18. }
  19.  
  20. require_once "connect.php";
  21.  
  22. $polaczenie = @new mysqli($host, $db_user, $db_password, $db_name);
  23. if ($polaczenie->connect_errno!=0)
  24. {
  25. echo "Error: ".$polaczenie->connect_errno;
  26. }
  27. else
  28. {
  29.  
  30. $login = $_POST['login'];
  31. $pass = $_POST['pass'];
  32.  
  33. $login = htmlentities($login, ENT_QUOTES, "utf8");
  34. $pass = htmlentities($pass, ENT_QUOTES, "utf8");
  35.  
  36. $pass = user::passSalter($pass);
  37.  
  38. if ($rezultat = @$polaczenie->query(
  39. sprintf("SELECT * FROM uzytkownicy WHERE user='%s' AND pass='%s'",
  40. mysqli_real_escape_string($polaczenie,$login),
  41. mysqli_real_escape_string($polaczenie,$pass))))
  42. {
  43. $ilu_userow = $rezultat->num_rows;
  44. if($ilu_userow>0)
  45. {
  46. $_SESSION['zalogowany'] = true;
  47.  
  48. $wiersz = $rezultat->fetch_assoc();
  49. $_SESSION['id'] = $wiersz['id'];
  50. $_SESSION['user'] = $wiersz['user'];
  51. $_SESSION['email'] = $wiersz['email'];
  52. $_SESSION['imie'] = $wiersz['imie'];
  53. $_SESSION['nazwisko'] = $wiersz['nazwisko'];
  54.  
  55. unset($_SESSION['blad']);
  56. $rezultat->free_result();
  57. header('Location: /panel,pl.html');
  58.  
  59. } else {
  60.  
  61. $_SESSION['blad'] = '<table border="2" style="border-style: none;" width="500" align="center">
  62. <tr>
  63. <th style="border-color: #6495ED; background-color: #FAF0E6; -moz-border-radius: 6px; border-style: solid;" align="center">
  64. <b>Nieprawidłowy login lub hasło!</b></th>
  65. </tr>
  66. </table><br />';
  67. header('Location: /panel,pl.html');
  68.  
  69. }
  70.  
  71. }
  72.  
  73. $polaczenie->close();
  74. }
  75.  
  76. ?>


Czy ktoś widzi gdzieś problem? Bo ja już nie daje rady.

Ten post edytował Mlody993 8.03.2016, 20:42:05
Go to the top of the page
+Quote Post
nospor
post 8.03.2016, 21:32:59
Post #2





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




Wywal te htmlentities... skądże to wytzasnal?


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
Mlody993
post 11.03.2016, 14:05:33
Post #3





Grupa: Zarejestrowani
Postów: 81
Pomógł: 0
Dołączył: 12.02.2009

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


W drugim temacie gdzie miałem inny problem ruszyłem lekko też ten temat kodowania:
http://forum.php.pl/index.php?act=findpost&pid=1186250

No i pewien użytkownik właśnie mi to podpowiedział, jednak nie pomogło. Ale gdy to usunę to też nie działają polskie znaki przy logowaniu sad.gif

Nikt nie potrafi mi pomóc?
Go to the top of the page
+Quote Post
Rufus_84
post 11.03.2016, 14:11:39
Post #4





Grupa: Zarejestrowani
Postów: 77
Pomógł: 1
Dołączył: 15.12.2012

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


Spróbuj ustawić kodowanie w ten sposób mysqli_set_charset($polaczenie,"utf8");
Go to the top of the page
+Quote Post
Mlody993
post 11.03.2016, 23:13:13
Post #5





Grupa: Zarejestrowani
Postów: 81
Pomógł: 0
Dołączył: 12.02.2009

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


Niestety nie działa, albo dodaje to w złym miejscu...

  1.  
  2. if ($rezultat = @$polaczenie->query(
  3. sprintf("SELECT * FROM uzytkownicy WHERE user='%s' AND pass='%s'",
  4. mysqli_set_charset($polaczenie,"utf8");
  5. mysqli_real_escape_string($polaczenie,$login),
  6. mysqli_real_escape_string($polaczenie,$pass))))
  7. {
Go to the top of the page
+Quote Post
Rufus_84
post 12.03.2016, 14:12:40
Post #6





Grupa: Zarejestrowani
Postów: 77
Pomógł: 1
Dołączył: 15.12.2012

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


Spróbuj w tym miejscu:

  1. $polaczenie = @new mysqli($host, $db_user, $db_password, $db_name);
  2. if ($polaczenie->connect_errno!=0)
  3. {
  4. echo "Error: ".$polaczenie->connect_errno;
  5. }
  6. else
  7. {
  8. mysqli_set_charset($polaczenie,"utf8");
  9.  
  10.  
  11.  
  12.  
Go to the top of the page
+Quote Post
Mlody993
post 12.03.2016, 17:28:33
Post #7





Grupa: Zarejestrowani
Postów: 81
Pomógł: 0
Dołączył: 12.02.2009

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


Nie no to jest jakieś dziwne biggrin.gif Dziękuje bardzo kolego!!

Wstawiłem tak:
  1. $polaczenie = @new mysqli($host, $db_user, $db_password, $db_name);
  2. if ($polaczenie->connect_errno!=0)
  3. {
  4. echo "Error: ".$polaczenie->connect_errno;
  5. }
  6. else
  7. {
  8. mysqli_set_charset($polaczenie,"utf8");
  9. $login = $_POST['login'];
  10. $pass = $_POST['pass'];


No i teraz działa wszystko elegancko biggrin.gif

Dziwne... wcześniej próbowałem też:
  1. {
  2.  
  3. $login = $_POST['login'];
  4. $pass = $_POST['pass'];
  5. mysqli_set_charset($polaczenie,"utf8");


No i taka opcja niestety nie działała. biggrin.gif

Problem rozwiązany, bardzo wam dziękuję.


@edit:
No i niestety powstał kolejny problem...

Mam dwa pliki, a w nich kody.. plik ocen.php:
  1. <?php
  2.  
  3. if (isset($_POST['submit'])){
  4. $id = $_GET['id'];
  5. if(isset($_SESSION['user'])){
  6. $autor = $_SESSION['user'];
  7. $userid = $_SESSION['id'];
  8. }else{
  9. if(isset($_POST['autor']) AND !empty($_POST['autor'])){
  10. $autor = 'Anonim';
  11. }else{
  12. error(); //lub co� i powr�t
  13. }
  14. }
  15.  
  16. //Tytu� i Tre�� b�dzie wymagana wi�c nie musisz sprawdza� !empty
  17. $tytul = trim(htmlspecialchars(addslashes($_POST['tytul'])));
  18. $tresc = trim(htmlspecialchars(addslashes($_POST['tresc'])));
  19. $set_typ_coding = mysql_query("SET NAMES 'UTF-8'");
  20. $ip = $_POST['ip'];
  21.  
  22. require_once "connect.php";
  23.  
  24. $sql = "insert into komentarze (`tytul`, `autor`, `nr`, `userid`, `tresc`, `artid`, `ip`, `dodany`) values ('$tytul', '$autor', '0', '$userid', '$tresc', 'oceny', '$ip', NOW())";
  25. $wynik = mysql_query($sql, $polaczenie) or die(mysql_error());
  26. echo "<table border=\"2\" style=\"border-style: none;\" width=\"500\" align=\"center\"><tr><th style=\"border-color: #6495ED; background-color: #FAF0E6; -moz-border-radius: 6px; border-style: solid;\" align=\"center\"><b><font color=green>Twój komentarz został dodany!</font></b></th></tr></table><br />";
  27. }else{
  28.  
  29. if($zapytanie_mysql['autor'] == NULL){
  30. $autor = 'Anonim';
  31. }else{
  32. $autor = $zapytanie_mysql['autor'];
  33. }
  34.  
  35.  
  36. echo '<div class=\"qw\"><form method="post" action="">
  37. <table>
  38. <tr><td><input type="hidden" name="ip" value="'.$_SERVER['REMOTE_ADDR'].'" /></td></tr>
  39. <tr><td><input type="hidden" name="tytul" value="0" required/></td></tr>
  40.  
  41. <tr><td><input type="text" name="autor" value="'.$autor.'" readonly="readonly" onFocus="clearText" style="color:#39759B; font-weight: bold;" hidden/></td></tr>
  42.  
  43. <tr><td>Treść:</td></tr>
  44. <tr><td><textarea name="tresc" rows="5" cols="40" style="color:#39759B; font-weight: bold;" required>'.$_POST['tresc'].'</textarea></td></tr>
  45. </table>
  46. <table border="1" style="border-style: none;" width="20" align="center">
  47. <tr>
  48. <th style="border-color: #6495ED; background-color: #FAF0E6; -moz-border-radius: 6px; border-style: solid;" width="100" align="center">
  49. <input type="submit" name="submit" value="Dodaj komentarz" /></th>
  50.  
  51. </form>
  52. </th>
  53. </tr>
  54. </table>
  55.  
  56. </form></div>';
  57. }
  58. ?>


A tutaj plik news.php:
  1. <?php
  2.  
  3. if (isset($_POST['submit'])){
  4. $id = $_GET['id'];
  5. if(isset($_SESSION['user'])){
  6. $autor = $_SESSION['user'];
  7. $userid = $_SESSION['id'];
  8. }else{
  9. if(isset($_POST['autor']) AND !empty($_POST['autor'])){
  10. $autor = 'Anonim';
  11. }else{
  12. error(); //lub co� i powr�t
  13. }
  14. }
  15.  
  16. //Tytu� i Tre�� b�dzie wymagana wi�c nie musisz sprawdza� !empty
  17. $tytul = trim(htmlspecialchars(addslashes($_POST['tytul'])));
  18. $tresc = trim(htmlspecialchars(addslashes($_POST['tresc'])));
  19. $set_typ_coding = mysql_query("SET NAMES 'UTF-8'");
  20. $ip = $_POST['ip'];
  21.  
  22. require_once "connect.php";
  23.  
  24. $sql = "insert into komentarze (`tytul`, `autor`, `nr`, `userid`, `tresc`, `artid`, `ip`, `dodany`) values ('$tytul', '$autor', '0', '$userid', '$tresc', '$id', '$ip', NOW())";
  25. $wynik = mysql_query($sql, $polaczenie) or die(mysql_error());
  26. echo "<table border=\"2\" style=\"border-style: none;\" width=\"500\" align=\"center\"><tr><th style=\"border-color: #6495ED; background-color: #FAF0E6; -moz-border-radius: 6px; border-style: solid;\" align=\"center\"><b><font color=green>Twoja opinia została dodana!</font></b></th></tr></table><br />";
  27. }else{
  28.  
  29. if($zapytanie_mysql['autor'] == NULL){
  30. $autor = 'Anonim';
  31. }else{
  32. $autor = $zapytanie_mysql['autor'];
  33. }
  34.  
  35.  
  36. echo '<div class=\"qw\"><form method="post" action="">
  37. <table>
  38. <tr><td><input type="hidden" name="ip" value="'.$_SERVER['REMOTE_ADDR'].'" /></td></tr>
  39. <tr><td><input type="hidden" name="tytul" value="1" required/></td></tr>
  40.  
  41. <tr><td><input type="text" name="autor" value="'.$autor.'" readonly="readonly" onFocus="clearText" style="color:#39759B; font-weight: bold;" hidden/></td></tr>
  42.  
  43. <tr><td>Treść:</td></tr>
  44. <tr><td><textarea name="tresc" rows="5" cols="40" style="color:#39759B; font-weight: bold;" required>'.$_POST['tresc'].'</textarea></td></tr>
  45. </table>
  46. <table border="1" style="border-style: none;" width="20" align="center">
  47. <tr>
  48. <th style="border-color: #6495ED; background-color: #FAF0E6; -moz-border-radius: 6px; border-style: solid;" width="100" align="center">
  49. <input type="submit" name="submit" value="Dodaj opinię" /></th>
  50.  
  51. </form>
  52. </th>
  53. </tr>
  54. </table>
  55.  
  56. </form></div>';
  57. }
  58. ?>



Oba kody są jakby podobne. Ale problem jest taki dziwny, że po zalogowaniu w pliku news.php można dawać komentarze z polskimi znakami i wszystko jest poprawnie w bazie i na stronie. Jeśli chodzi o plik ocen.php to po zalogowaniu do bazy zostają zapisane polskie znaki w złym jakby kodowaniu :/ Zamiast ń w bazie i na stronie pojawia się: Ĺ i tak jest z każdym polskim znakiem. ;/ Jeśli jestem wylogowany to w obu przypadkach polskie znaki działają poprawnie i w bazie i na stronie. ;/

Ten post edytował Mlody993 12.03.2016, 18:31:46
Go to the top of the page
+Quote Post
Rufus_84
post 17.03.2016, 14:37:03
Post #8





Grupa: Zarejestrowani
Postów: 77
Pomógł: 1
Dołączył: 15.12.2012

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


Czemu używasz mysql a w poprzednim używałeś mysqli ? W ogóle nie powinno się uzywac już mysql. Spróbuj przepisać wszystko z mysqli.
Go to the top of the page
+Quote Post
com
post 18.03.2016, 23:48:05
Post #9





Grupa: Zarejestrowani
Postów: 3 034
Pomógł: 366
Dołączył: 24.05.2012

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


może byś tak poprawił kodowanie tych plików co wink.gif
Go to the top of the page
+Quote Post

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

 



RSS Wersja Lo-Fi Aktualny czas: 24.07.2025 - 12:28