Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: php logowanie
Forum PHP.pl > Forum > PHP
czarkowy
Siemka,
zrobiłem logowanie w php i jest mały problem, otóż za każdym raze jest niepoprawne logowanie, nie ważne czy dobry login i hasło czy zły

index.php
  1. <!DOCTYPE html>
  2. <html lang="pl">
  3. <meta charset="UTF-8">
  4. <title>Home Page</title>
  5. <link rel="stylesheet" href="index.css">
  6. <link rel="stylesheet" href="register.css">
  7. </head>
  8.  
  9. <form class="form-contener" action="signin.php" mathod="POST">
  10. <input type="text" class="inputText" name="username" placeholder="Your username">
  11. <input type="password" class="inputText" name="pass" placeholder="Your password">
  12. <button type="submit" class="inputSubmit" name="login">Sign In</button>
  13. </form>
  14.  
  15. </body>
  16. </html>


signin.php
  1. <?php
  2.  
  3. include 'db_connect.php';
  4.  
  5. $username = isset($_POST['username']) ? $_POST['username'] : "";
  6. $pass = isset($_POST['pass']) ? $_POST['pass'] : "";
  7.  
  8. $sql = "SELECT * FROM users WHERE username = '$username' AND pass = '$pass'";
  9.  
  10. $result = mysqli_query($conn, $sql);
  11.  
  12. if(!$row = mysqli_fetch_assoc($result)) {
  13. echo "Niezalogowany";
  14. } else {
  15. echo "Zalogowany";
  16. }


o co moze chodzic? :_:
viking
Sprawdz co zwraca wynik zapytania. Dodatkowo powinieneś bindowac parametry. A hasło przez password_hash.
czarkowy
Cytat(viking @ 6.08.2017, 16:48:47 ) *
Sprawdz co zwraca wynik zapytania. Dodatkowo powinieneś bindowac parametry. A hasło przez password_hash.


bindowac parametry tzn?

i z hashowaniem to wiem, ale po co to teraz robić jak nic nawet nie dziala ;p

dodałem var_dump($result);
i zwraca mi to:
  1. E:\Programy\wamp64\www\testproject\signin.php:17:
  2. object(mysqli_result)[2]
  3. public 'current_field' => int 0
  4. public 'field_count' => int 5
  5. public 'lengths' => null
  6. public 'num_rows' => int 0
  7. public 'type' => int 0


w ogóle nie wiem czy to jakoś źle dane nie sa przesylane ;p
bo var_dump($username) i var_dump($pass) zwracają mi puste stringi

@edit

$username = isset($_POST['username']) ? $_POST['username'] : "";
$pass = isset($_POST['pass']) ? $_POST['pass'] : "";

tutaj chyba w ogóle username i pass nie sa rozpoznawane i po prostu program wybiera ostatnia opcje czyli pustego stringa ;x bo jak tam wpisalem poprawne dane to mozna sie zalogowac
viking
Z tego wynika ze jest0 rekordów. Zrób var_dump($_POST).
czarkowy
Cytat(viking @ 6.08.2017, 17:16:45 ) *
Z tego wynika ze jest0 rekordów. Zrób var_dump($_POST).


var_dump($_POST) zwraca:
  1. E:\Programy\wamp64\www\testproject\signin.php:19:
  2. array (size=0)
markuz
Masz literówkę, nie mathod tylko method (w formularzu html).
czarkowy
Cytat(markuz @ 6.08.2017, 17:31:17 ) *
Masz literówkę, nie mathod tylko method (w formularzu html).


...od 2 dni z tym się głowie i walcze...a tu takie małe gówienko..uhh
dzięki wielkie smile.gif
viking
Tak czy inaczej zastosuj http://php.net/manual/en/mysqli-stmt.bind-param.php bo teraz masz otwarty skrypt na ataki.
czarkowy
Cytat(viking @ 6.08.2017, 18:08:53 ) *
Tak czy inaczej zastosuj http://php.net/manual/en/mysqli-stmt.bind-param.php bo teraz masz otwarty skrypt na ataki.


tak to powinno wyglądać? :

  1. <?php
  2.  
  3. if(isset($_SESSION['name'])){
  4. header('Location: index.php');
  5. }
  6.  
  7. include 'db_connect.php';
  8.  
  9. $firstname = isset($_POST['firstname']) ? $_POST['firstname'] : "";
  10. $username = isset($_POST['username']) ? $_POST['username'] : "";
  11. $email = isset($_POST['email']) ? $_POST['email'] : "";
  12. $pass = isset($_POST['pass']) ? $_POST['pass'] : "";
  13. $pass2 = isset($_POST['pass2']) ? $_POST['pass2'] : "";
  14.  
  15. if($pass == $pass2){
  16.  
  17. $username = addslashes($username);
  18. $pass = addslashes($pass);
  19.  
  20. $username = mysqli_real_escape_string($conn, $username);
  21. $pass = mysqli_real_escape_string($conn, $pass);
  22.  
  23. $pass = hash('sha256', '<>?/!@#$%^&*()-=+:'.$pass);
  24.  
  25. $stmt = mysqli_prepare($conn, "INSERT INTO users VALUES(?, ?, ?, ?)");
  26. mysqli_stmt_bind_param($stmt, $firstname, $username, $email, $pass);
  27.  
  28. /*$sql = "INSERT INTO users (firstname, username, email, pass) VALUES ('$firstname', '$username', '$email', '$pass')";
  29. $result = $conn->query($sql);
  30. */
  31. mysqli_stmt_execute($stmt);
  32.  
  33. $_SESSION['name'] = 'session: '.$username;
  34. $_SESSION['username'] = $username;
  35.  
  36. header("Location: index.php");
  37. }
  38.  
  39. mysqli_stmt_close($stmt);
  40. mysqli_close($conn);
  41.  
viking
W dokumentacji to na pewno tak nie wygląda więc masz odpowiedź. Wywal te add_slashes i escape stri ng dodatkowo.
czarkowy
Cytat(viking @ 7.08.2017, 21:43:41 ) *
W dokumentacji to na pewno tak nie wygląda więc masz odpowiedź. Wywal te add_slashes i escape stri ng dodatkowo.


dziwne to jest ;p

takie coś? :

  1. <?php
  2.  
  3. if(isset($_SESSION['name'])){
  4. header('Location: index.php');
  5. }
  6.  
  7. include 'db_connect.php';
  8.  
  9. $firstname = isset($_POST['firstname']) ? $_POST['firstname'] : "";
  10. $username = isset($_POST['username']) ? $_POST['username'] : "";
  11. $email = isset($_POST['email']) ? $_POST['email'] : "";
  12. $pass = isset($_POST['pass']) ? $_POST['pass'] : "";
  13. $pass2 = isset($_POST['pass2']) ? $_POST['pass2'] : "";
  14.  
  15. if($pass == $pass2){
  16.  
  17. $pass = hash('sha256', '<>?/!@#$%^&*()-=+:'.$pass);
  18.  
  19. $stmt = mysqli_prepare($conn, "INSERT INTO users VALUES (?, ?, ?, ?)");
  20. mysqli_stmt_bind_param($stmt, 'ss', $firstname_stmt, $username_stmt, $email_stmt, $pass_stmt);
  21.  
  22. $firstname_stmt = $firstname;
  23. $username_stmt = $username;
  24. $email_stmt = $email;
  25. $pass_stmt = $pass;
  26.  
  27. mysqli_stmt_execute();
  28.  
  29. /*$sql = "INSERT INTO users (firstname, username, email, pass) VALUES ('$firstname', '$username', '$email', '$pass')";
  30. $result = $conn->query($sql);
  31. */
  32. $_SESSION['name'] = 'session: '.$username_stmt;
  33. $_SESSION['username'] = $username_stmt;
  34.  
  35. header("Location: index.php");
  36. }
  37.  
  38. mysqli_stmt_close($stmt);
  39. mysqli_close($conn);
  40.  

viking
Uruchomiłeś ten kod? Z dokumentacji: A string that contains one or more characters which specify the types for the corresponding bind variables. Ile masz bind values? Ja tam widzę 4. Najpierw podstawiasz zmienne, potem je tworzysz.
Definicja: bool mysqli_stmt_execute ( mysqli_stmt $stmt )
czarkowy
Cytat(viking @ 8.08.2017, 06:42:02 ) *
Uruchomiłeś ten kod? Z dokumentacji: A string that contains one or more characters which specify the types for the corresponding bind variables. Ile masz bind values? Ja tam widzę 4. Najpierw podstawiasz zmienne, potem je tworzysz.
Definicja: bool mysqli_stmt_execute ( mysqli_stmt $stmt )


no uruchamiam i kurde działa, wlasnie gdyby jakies bledy wyskakiwaly to łatwiej by to szlo ;p

teraz takie coś mam:

  1. <?php
  2.  
  3. if(isset($_SESSION['name'])){
  4. header('Location: index.php');
  5. }
  6.  
  7. include 'db_connect.php';
  8.  
  9. $stmt = mysqli_prepare($conn, "INSERT INTO users(firstname, username, email, pass) VALUES (?, ?, ?, ?)");
  10. mysqli_stmt_bind_param($stmt, 'ssss', $firstname_stmt, $username_stmt, $email_stmt, $pass_stmt);
  11.  
  12. $firstname_stmt = isset($_POST['firstname']) ? $_POST['firstname'] : "";
  13. $username_stmt = isset($_POST['username']) ? $_POST['username'] : "";
  14. $email_stmt = isset($_POST['email']) ? $_POST['email'] : "";
  15. $pass_stmt = isset($_POST['pass']) ? $_POST['pass'] : "";
  16. $pass2 = isset($_POST['pass2']) ? $_POST['pass2'] : "";
  17.  
  18. $pass_stmt = hash('sha256', '<>?/!@#$%^&*()-=+:'.$pass_stmt);
  19.  
  20. mysqli_stmt_execute($stmt);
  21.  
  22. $_SESSION['name'] = 'session: '.$username_stmt;
  23. $_SESSION['username'] = $username_stmt;
  24.  
  25. header("Location: index.php");
  26.  
  27. mysqli_stmt_close($stmt);
  28. mysqli_close($conn);
  29.  
viking
A jak ma wywalić błędy skoro robisz przekierowanie
czarkowy
Cytat(viking @ 8.08.2017, 11:54:49 ) *
A jak ma wywalić błędy skoro robisz przekierowanie


wywalilem i tez nie ma zadnych

to wyzej jest okej czy nie?
nospor
Cytat
no uruchamiam i kurde działa,
Sorki, ze sie wcinam w te interesujaca dyskusje ale skoro dziala to w czym problem?
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.