页面刷新后会话被销毁
Session gets destroyed after page refresh
Login.php
<?php
session_start();
$server = "localhost";
$user = "...";
$pass = "...";
$database = "...";
$verbindung = mysqli_connect($server, $user, $pass, $database)
or die("Verbindung konnte nicht hergestellt werden.");
$Email = $_POST["Email"];
$Passwort = $_POST["Passwort"];
$sql = "SELECT passwort FROM accounts WHERE email = '".$Email."'";
$hashPasswort = mysqli_query($verbindung, $sql);
$VerifyHash = mysqli_fetch_assoc($hashPasswort);
if(password_verify($Passwort, $VerifyHash['passwort']))
{
session_regenerate_id();
$_SESSION['email'] = $Email;
echo "<script type='text/javascript'>
window.location.replace('...');
</script>";
}
else
{
echo '<script type="text/javascript">
window.location.replace("...");
</script>';
}
$return = mysqli_close($verbindung);
if (!$return) {
echo "<p>Die Verbindung mit dem Server konnte nicht geschlossen werden.</p>";
}
?>
Index.php
<?php
session_start();
if (!isset($_SESSION['email'])) {
header('Location: Login.php?login=loginRequired');
exit;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta name="viewport" content="width=device-width, initial-scale=0.85">
<link rel="icon" type="image/png" href="../Bilder/favicon.ico"/>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<title>Erste-Hilfe Kurs</title>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<link rel='stylesheet' type='text/css' media='screen' href='../CSS/main.css'>
<link rel='stylesheet' type='text/css' media='screen' href='../CSS/index.css'>
</head>
<body>
<header>
<div class="container">
<a href="https://www.litec.ac.at/"><img src="../Bilder/Litec.png" alt="Litec" class="logo"></a>
<a href="https://www.roteskreuz.at/home/"><img src="../Bilder/RotesKreuz2.png" alt="Litec" class="logo"></a>
<nav>
<ul>
<li><a href="Index.php"><b><u>Home</u></b></a></li>
<li><a href="UeberUns.php">Über uns</a></li>
<li><a href="Anmeldung.php">Anmeldung</a></li>
<li><a href="Impressum.php">Impressum</a></li>
<li><a href="<?php session_destroy();?> Login.php">LOGOUT</a></li>
</ul>
</nav>
</div>
</header>
<label id="email"></label>
<?php echo("{$_SESSION['email']}"."<br />");?>
</body>
</html>
我制作了一个登录脚本,如果密码正确,它会打开 Index.php 站点。首次通过登录脚本打开 Index.php 站点时,一切正常,会话已设置。但是在我刷新页面后,会话被破坏并且没有设置。
那么如何保存会话,使其不会因刷新浏览器而被破坏?
您 index.php
中的这一行会破坏您的会话:
<li><a href="<?php session_destroy();?> Login.php">LOGOUT</a></li>
^^^^^^^^^^^^^^^^^
要实现注销过程,您可以 link 到另一个 php 文件并在那里执行 session_destroy();
- 例如。
Login.php
<?php
session_start();
$server = "localhost";
$user = "...";
$pass = "...";
$database = "...";
$verbindung = mysqli_connect($server, $user, $pass, $database)
or die("Verbindung konnte nicht hergestellt werden.");
$Email = $_POST["Email"];
$Passwort = $_POST["Passwort"];
$sql = "SELECT passwort FROM accounts WHERE email = '".$Email."'";
$hashPasswort = mysqli_query($verbindung, $sql);
$VerifyHash = mysqli_fetch_assoc($hashPasswort);
if(password_verify($Passwort, $VerifyHash['passwort']))
{
session_regenerate_id();
$_SESSION['email'] = $Email;
echo "<script type='text/javascript'>
window.location.replace('...');
</script>";
}
else
{
echo '<script type="text/javascript">
window.location.replace("...");
</script>';
}
$return = mysqli_close($verbindung);
if (!$return) {
echo "<p>Die Verbindung mit dem Server konnte nicht geschlossen werden.</p>";
}
?>
Index.php
<?php
session_start();
if (!isset($_SESSION['email'])) {
header('Location: Login.php?login=loginRequired');
exit;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta name="viewport" content="width=device-width, initial-scale=0.85">
<link rel="icon" type="image/png" href="../Bilder/favicon.ico"/>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<title>Erste-Hilfe Kurs</title>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<link rel='stylesheet' type='text/css' media='screen' href='../CSS/main.css'>
<link rel='stylesheet' type='text/css' media='screen' href='../CSS/index.css'>
</head>
<body>
<header>
<div class="container">
<a href="https://www.litec.ac.at/"><img src="../Bilder/Litec.png" alt="Litec" class="logo"></a>
<a href="https://www.roteskreuz.at/home/"><img src="../Bilder/RotesKreuz2.png" alt="Litec" class="logo"></a>
<nav>
<ul>
<li><a href="Index.php"><b><u>Home</u></b></a></li>
<li><a href="UeberUns.php">Über uns</a></li>
<li><a href="Anmeldung.php">Anmeldung</a></li>
<li><a href="Impressum.php">Impressum</a></li>
<li><a href="<?php session_destroy();?> Login.php">LOGOUT</a></li>
</ul>
</nav>
</div>
</header>
<label id="email"></label>
<?php echo("{$_SESSION['email']}"."<br />");?>
</body>
</html>
我制作了一个登录脚本,如果密码正确,它会打开 Index.php 站点。首次通过登录脚本打开 Index.php 站点时,一切正常,会话已设置。但是在我刷新页面后,会话被破坏并且没有设置。 那么如何保存会话,使其不会因刷新浏览器而被破坏?
您 index.php
中的这一行会破坏您的会话:
<li><a href="<?php session_destroy();?> Login.php">LOGOUT</a></li>
^^^^^^^^^^^^^^^^^
要实现注销过程,您可以 link 到另一个 php 文件并在那里执行 session_destroy();
- 例如。