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<?php
function checkPass($user, $pass)
{
if(!$fd = fopen("passwords.txt", "r")) return 1; $result = 2;
if(count($arr) < 2
) continue
;
if($arr[0] != $user) continue;
if($arr[1] == $pass){
$result = 0;
}
break;
}
return $result;
}
if(isset($_SESSION['zalogowany'])){ }
else if(!isset($_POST["haslo"]) || !isset($_POST["user"])){ $_SESSION['komunikat'] = "Wprowadź nazwę i hasło użytkownika.";
include('form.php');
}
else{
$val = checkPass($_POST["user"], $_POST["haslo"]);
if($val == 0){
$_SESSION['zalogowany'] = $_POST["user"];
}
else if($val == 1){
$_SESSION['komunikat'] = "Błąd serwera. Zalogowanie nie było możliwe.";
include('form.php');
}
else{
$_SESSION['komunikat'] = "Błąd serwera. Zalogowanie nie było możliwe.";
include('form.php');
}
}
?>
form.php<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Logowanie</title>
</head>
<body>
<div>
<div style="font-size:16pt">
<?php
if(isset($_SESSION['komunikat'])) echo $_SESSION['komunikat']; else
echo "Wprowadź nazwę i hasło użytkownika."; ?>
</div>
<form action = "login.php" methood = "POST">
<table>
<tr>
<td>Użytkownik:</td>
<td>
<input type="text" name="user">
</td>
</tr>
<tr>
<td>Hasło:</td>
<td>
<input type="password" name="haslo">
</td>
</tr>
<tr>
<td colspan="2" style="text-align:center;">
<input type="submit" value="Zaloguj">
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
main.php<?php
if(!isset($_SESSION['zalogowany'])){ $_SESSION['komunikat'] = "Nie jesteś zalogowany!";
include('form.php');
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Lista plików</title>
</head>
<body>
Jesteś zalogowany jako:
<?php echo $_SESSION['zalogowany'] ?><br />
<a href="logout.php">Wyloguj</a>
</body>
</html>
logout.php<?php
if(!isset($_SESSION['zalogowany'])){ $komunikat = "Nie jesteś zalogowany!";
}
else{
unset($_SESSION['zalogowany']); $komunikat = "Wylogowanie prawidłowe!";
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Wylogowanie</title>
</head>
<body>
</body>
</html>
passwords.txtadmin:qwerty
Ten post edytował Boryna 23.08.2013, 13:10:47