****Witam,
Posiadam 4 pliki php.
1.index.php
2.login.php
3.welcome.php
4.logout.php
W pliku "index.php" jest zmienna tablicy superglobalnej $_SESSION['error'], która powinna pokazywac błąd w razie błędnego wypełnienia formularza. Nie pokazuje tego błędu.
Gdy jestem w pliku "welcome.php" i gdy wpiszę "index.php" z adresu przeglądarki, to przekierowuje mnie do tej strony, a nie powinno.
Jest tam przekierowanie, które powinno mnie przekierować do "index.php".
Gdy wpiszę nick i hasło, to przekierowuje mnie do pliku login.php, zamiast do pliku welcome.php
Proszę o sprawdzenie kodu.
Oto mój kod:
1.index.php
<?php
if((isset($_SESSION['zalogowany']) && $_SESSION['zalogowany']==true)){
header('Location:welcome.php');
exit();
}
?>
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="style.css"/>
<link href="https://fonts.googleapis.com/css?family=Roboto+Mono: 400,700&display=swap&subset=latin-ext" rel="stylesheet">
<title>Main site</title>
</head>
<body>
<div id="container">
<form action="login.php" method="post" id="form">
Put your nick:<br/>
<input type="text" name="nick"/><br/>
Put your password:<br/>
<input type="password" name="password"/><br/>
<input type="submit" value="Login" name="submit"/><br/><br/>
</form>
<?php
if(isset($_SESSION['error'])){
$_SESSION['error'];
}
?>
</div>
</body>
</html>
2.login.php
<?php
session_start();
if((!isset($_POST['nick'])) || (!isset($_POST['password']))){
header('Location:index.php');
exit();
}
if (isset($nick) && isset($password)){
if($nick == "zibi" && $password == "admin"){
$_SESSION['nick'] = $nick;
$_SESSION['password'] = $password;
$_SESSION['zalogowany'] = true;
unset($_SESSION['error']);
header('Location:welcome.php');
}else{
$_SESSION['error'] = '<span style="color:red">Wrong data</span>';
header('Location:index.php');}
}else{
if((!isset($_POST['nick'])) || (!isset($_POST['password'])))
header('Location:index.php');
}
?>
- welcome.php
<?php
session_start();
if(!isset($_SESSION['zalogowany'])){
header('Location:index.php');
exit();
}
?>
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="style.css"/>
<link href="https://fonts.googleapis.com/css?family=Roboto+Mono: 400,700&display=swap&subset=latin-ext" rel="stylesheet">
<title>Welcome my friend</title>
</head>
<body>
<div id="container">
<div id="wiadomosc">
<?php
if((isset($_SESSION['nick']) && isset($_SESSION['password']))){
echo "Hello ".$_SESSION['nick']."<br/>";
echo "Your password: ".$_SESSION['password']."<br/>";
}else{
header('Location:index.php');
}
echo '<a href="logout.php">Logout</a>';
?>
</div>
</div>
</body>
</html>
- logout.php
<?php
session_start();
session_unset();
header('Location:index.php');