Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP]Logowanie
Mastroeni7
post 20.03.2011, 01:56:12
Post #1





Grupa: Zarejestrowani
Postów: 6
Pomógł: 0
Dołączył: 22.02.2011

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


Chcę, aby mój skrypt przekierowywał admina na inną stronę, oraz użytkownika na inną. Problemem jest to, że co nie wpiszę przechodzi mi tylko do 'user.php' Może powiecie co jest nie tak ? Na początku jeszcze wyskakuje mi błąd Notice: Undefined index: login in /home/***//logowanie.php on line 10
  1. <?php
  2.  
  3. include ('db.php');
  4. function clean($value) {
  5. if(get_magic_quotes_gpc()) $value = stripslashes($value);
  6. return trim(mysql_real_escape_string($value));
  7. }
  8.  
  9. if($_POST['login'] && $_POST['username'] && $_POST['password']) {
  10. $username = clean($_POST['username']);
  11. $password = md5($_POST['password']);
  12.  
  13. $isAdmin = mysql_query("SELECT count(*) FROM users WHERE user_name = '$username' AND user_type = '1'");
  14. if(mysql_num_rows($isAdmin) == 1) {
  15. header("Location: admin.php");
  16. }
  17.  
  18. $result = mysql_query("SELECT count(*) FROM users WHERE user_name = '$username' AND user_password = '$password' and user_type='2'");
  19. if(mysql_num_rows($result) == 1) {
  20. $_SESSION['username'] = $username;
  21. header("Location: user.php");
  22. } else {
  23. header("Location: opps.php");
  24. }
  25. }
  26.  
  27.  
  28. ?>
  29. formularz w htmlu

user_type = 1, mam w mysql dla admina, user_type = 2, dla użytkownika
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 8)
CuteOne
post 20.03.2011, 02:49:31
Post #2





Grupa: Zarejestrowani
Postów: 2 958
Pomógł: 574
Dołączył: 23.09.2008
Skąd: wiesz, że tu jestem?

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


1. Błąd dotyczy braku (lub złej nazwy) <input name="login"> - właściwie to po co ci ta zmienna skoro jej nie używasz?questionmark.gif?
2. Sprawdzaj czy zmienne $_POST są puste a nie czy istnieją.
3. eee... wystarczy podać login i już zostajesz przekierowany na stronę admina?

  1. if(!empty($_POST['username']) && !empty($_POST['password'])) {
  2.  
  3. $username = clean($_POST['username']);
  4. $password = md5($_POST['password']);
  5.  
  6. $query = mysql_query("SELECT password, user_type FROM users WHERE user_name = '$username'");
  7.  
  8. if(mysql_num_rows($query)) {
  9.  
  10. $row = mysql_fetch_array($query);
  11.  
  12. if($row['password'] == $password) {
  13.  
  14.  
  15. $_SESSION['username'] = $username;
  16.  
  17. switch($row['user_type']) {
  18.  
  19. case 1:
  20. $_SESSION['user_type'] = 1;
  21. header("Location: admin.php");
  22. break;
  23.  
  24. case 2:
  25. $_SESSION['user_type'] = 2;
  26. header("Location: opps.php");
  27. break;
  28.  
  29. default:
  30. header("Location: index.php");
  31. break;
  32. }
  33. }
  34. }
  35. }
  36.  
  37. header("Location: login.php");
  38.  


Skrypt ten nie uwzględnia wyświetlania błędów podczas złego wpisania loginu/hasła itp.

Ten post edytował CuteOne 20.03.2011, 02:52:19
Go to the top of the page
+Quote Post
Mastroeni7
post 20.03.2011, 09:58:24
Post #3





Grupa: Zarejestrowani
Postów: 6
Pomógł: 0
Dołączył: 22.02.2011

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


Zrobiłem tak jak napisałeś, ale formularza mi nawet nie wyświetla tylko od razu przekierowuje na stronę: login.php

Go to the top of the page
+Quote Post
CuteOne
post 20.03.2011, 12:11:06
Post #4





Grupa: Zarejestrowani
Postów: 2 958
Pomógł: 574
Dołączył: 23.09.2008
Skąd: wiesz, że tu jestem?

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


  1. if(!empty($_POST['username']) && !empty($_POST['password'])) {
  2.  
  3. $username = clean($_POST['username']);
  4. $password = md5($_POST['password']);
  5.  
  6. $query = mysql_query("SELECT password, user_type FROM users WHERE user_name = '$username'");
  7.  
  8. if(mysql_num_rows($query)) {
  9.  
  10. $row = mysql_fetch_array($query);
  11.  
  12. if($row['password'] == $password) {
  13.  
  14.  
  15. $_SESSION['username'] = $username;
  16.  
  17. switch($row['user_type']) {
  18.  
  19. case 1:
  20. $_SESSION['user_type'] = 1;
  21. header("Location: admin.php");
  22. break;
  23.  
  24. case 2:
  25. $_SESSION['user_type'] = 2;
  26. header("Location: opps.php");
  27. break;
  28.  
  29. default:
  30. header("Location: index.php");
  31. break;
  32. }
  33. }
  34. }
  35. else {
  36.  
  37. header("Location: login.php");
  38. }
  39. }
  40. else {
  41. header("Location: login.php");
  42. }
Go to the top of the page
+Quote Post
Mastroeni7
post 20.03.2011, 12:33:04
Post #5





Grupa: Zarejestrowani
Postów: 6
Pomógł: 0
Dołączył: 22.02.2011

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


Musiałem dodać:
  1. $query = mysql_query("SELECT user_password, user_type FROM users WHERE user_name = '$username' and user_password='$password'");

żeby gdziekolwiek przekierowywało, lecz dalej to samo, tylko do jednej strony: login.php. Jakby nie wykrywało kto się loguje po tym user_type. mad.gif

Ten post edytował Mastroeni7 20.03.2011, 12:43:26
Go to the top of the page
+Quote Post
CuteOne
post 20.03.2011, 16:21:54
Post #6





Grupa: Zarejestrowani
Postów: 2 958
Pomógł: 574
Dołączył: 23.09.2008
Skąd: wiesz, że tu jestem?

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


wstaw to
  1. echo '<pre>';
  2. print_r($_POST);
  3.  
  4. if(!empty($_POST['username']) && !empty($_POST['password'])) {
  5.  
  6. $username = clean($_POST['username']);
  7. $password = md5($_POST['password']);
  8.  
  9. $query = mysql_query("SELECT password, user_type FROM users WHERE user_name = '$username'") or die(mysql_error());
  10.  
  11. if(mysql_num_rows($query)) {
  12.  
  13. $row = mysql_fetch_array($query);
  14.  
  15. if($row['password'] == $password) {
  16.  
  17.  
  18. $_SESSION['username'] = $username;
  19.  
  20. switch($row['user_type']) {
  21.  
  22. case 1:
  23. $_SESSION['user_type'] = 1;
  24. header("Location: admin.php");
  25. break;
  26.  
  27. case 2:
  28. $_SESSION['user_type'] = 2;
  29. header("Location: opps.php");
  30. break;
  31.  
  32. default:
  33. header("Location: index.php");
  34. break;
  35. }
  36. }
  37. else {
  38.  
  39. echo 'Hasło jest niepoprawne';//header("Location: login.php");
  40. }
  41. }
  42. else {
  43.  
  44. echo 'Brak wyników do wyświetlenia';//header("Location: login.php");
  45. }
  46. }
  47. else {
  48. echo 'login lub hasło są puste';//header("Location: login.php");
  49. }


Ten post edytował CuteOne 20.03.2011, 16:26:05
Go to the top of the page
+Quote Post
Mastroeni7
post 20.03.2011, 20:09:03
Post #7





Grupa: Zarejestrowani
Postów: 6
Pomógł: 0
Dołączył: 22.02.2011

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


Teraz jest jeszcze dziwniej, juz przy wyświetlaniu formularza wywala mi błąd:
  1. (
  2. )
  3. login lub hasło sš puste


a jeśli wpisze albo nazwe admina i hasło przekierowuje mnie na:
  1. (
  2. [username] => Admin
  3. [password] => admin
  4. [login] => Click to Login
  5. )
  6. Hasło jest niepoprawne


a jeśli wpisze użytkownika jest tak samo ;/

Może jest na to jakiś inny sposób ?
Go to the top of the page
+Quote Post
CuteOne
post 21.03.2011, 01:45:39
Post #8





Grupa: Zarejestrowani
Postów: 2 958
Pomógł: 574
Dołączył: 23.09.2008
Skąd: wiesz, że tu jestem?

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


No i masz odpowiedz - wpisujesz złe hasło lub co bardziej prawdopodobne źle ustawiłeś długość kolumny password w bazie danych. Najlepiej ustaw tą kolumnę na varchar, długość 100
Go to the top of the page
+Quote Post
Mastroeni7
post 21.03.2011, 14:50:21
Post #9





Grupa: Zarejestrowani
Postów: 6
Pomógł: 0
Dołączył: 22.02.2011

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


Dzięki Ci wielkie, wszystko działa.Problemem było to,że podczas rejestracji miałem ustawione kodowanie sha1 a przy logowaniu md5. smile.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: 19.07.2025 - 06:21