Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP] Logowanie na plikach
Boryna
post
Post #1





Grupa: Zarejestrowani
Postów: 4
Pomógł: 0
Dołączył: 23.08.2013

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


Witam, usiłuję napisać prosty skrypt do logowania na stronę. Cały czas gdzieś jednak popełniam błąd i cały "system" nie chce działać, proszę o sprawdzenie.
- login.php - procedura logowania
- form.php - formularz logowania
- main.php - strona dostępna po zalogowaniu
- logout.php - procedura wylogowania
- passwords.txt - plik przechowujący dane logowania

login.php
  1. <?php
  2. function checkPass($user, $pass)
  3. {
  4. if(!$fd = fopen("passwords.txt", "r")) return 1;
  5. $result = 2;
  6. while (!feof ($fd)){
  7. $line = trim(fgets($fd));
  8. $arr = explode(":", $line);
  9. if(count($arr) < 2) continue;
  10.  
  11. if($arr[0] != $user) continue;
  12.  
  13. if($arr[1] == $pass){
  14. $result = 0;
  15. }
  16. break;
  17. }
  18. fclose($fd);
  19. return $result;
  20. }
  21.  
  22. if(isset($_SESSION['zalogowany'])){
  23. header("Location: main.php");
  24. }
  25. else if(!isset($_POST["haslo"]) || !isset($_POST["user"])){
  26. $_SESSION['komunikat'] = "Wprowadź nazwę i hasło użytkownika.";
  27. include('form.php');
  28. }
  29. else{
  30. $val = checkPass($_POST["user"], $_POST["haslo"]);
  31. if($val == 0){
  32. $_SESSION['zalogowany'] = $_POST["user"];
  33. header("Location: main.php");
  34. }
  35. else if($val == 1){
  36. $_SESSION['komunikat'] = "Błąd serwera. Zalogowanie nie było możliwe.";
  37. include('form.php');
  38. }
  39. else{
  40. $_SESSION['komunikat'] = "Błąd serwera. Zalogowanie nie było możliwe.";
  41. include('form.php');
  42. }
  43. }
  44. ?>


form.php
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <title>Logowanie</title>
  6. </head>
  7. <body>
  8. <div>
  9. <div style="font-size:16pt">
  10. <?php
  11. if(isset($_SESSION['komunikat']))
  12. echo $_SESSION['komunikat'];
  13. else
  14. echo "Wprowadź nazwę i hasło użytkownika.";
  15. ?>
  16. </div>
  17. <form action = "login.php" methood = "POST">
  18. <table>
  19. <tr>
  20. <td>Użytkownik:</td>
  21. <td>
  22. <input type="text" name="user">
  23. </td>
  24. </tr>
  25. <tr>
  26. <td>Hasło:</td>
  27. <td>
  28. <input type="password" name="haslo">
  29. </td>
  30. </tr>
  31. <tr>
  32. <td colspan="2" style="text-align:center;">
  33. <input type="submit" value="Zaloguj">
  34. </td>
  35. </tr>
  36. </table>
  37. </form>
  38. </div>
  39. </body>
  40. </html>


main.php
  1. <?php
  2. if(!isset($_SESSION['zalogowany'])){
  3. $_SESSION['komunikat'] = "Nie jesteś zalogowany!";
  4. include('form.php');
  5. exit();
  6. }
  7. ?>
  8. <!DOCTYPE html>
  9. <html>
  10. <head>
  11. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  12. <title>Lista plików</title>
  13. </head>
  14. <body>
  15. Jesteś zalogowany jako: <?php echo $_SESSION['zalogowany'] ?>
  16. <br />
  17. <a href="logout.php">Wyloguj</a>
  18. </body>
  19. </html>


logout.php
  1. <?php
  2. if(!isset($_SESSION['zalogowany'])){
  3. $komunikat = "Nie jesteś zalogowany!";
  4. }
  5. else{
  6. unset($_SESSION['zalogowany']);
  7. $komunikat = "Wylogowanie prawidłowe!";
  8. }
  9. ?>
  10. <!DOCTYPE html>
  11. <html>
  12. <head>
  13. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  14. <title>Wylogowanie</title>
  15. </head>
  16. <body>
  17. <?php echo $komunikat ?>
  18. </body>
  19. </html>


passwords.txt
admin:qwerty

Ten post edytował Boryna 23.08.2013, 13:10:47
Go to the top of the page
+Quote Post

Posty w temacie


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 Aktualny czas: 19.08.2025 - 14:38