ユーザがログインしている場所やその他の基本機能のコードを書いています。誰もがログインすると、ログアウトボタンは訪問したすべてのページに表示され、ログインしていない場合は非表示になります。私はまだ新しいので何が間違っているのか分からない。最初にログインボタンをクリックするとすぐにログアウトボタンが表示されますが、他のページに移動すると、そのメニューからボタンが消えます。ログインしたときのみ、ログアウトボタンを表示するPHP
menu.php
$currentfile = basename($_SERVER['PHP_SELF']);
if($currentfile = basename($_SERVER['PHP_SELF'])){
if (isset($_SESSION['ID'])) {
echo "<a href=\"index.php\">Home</a>
<a href=\"viewlist.php\">See Reviews</a>
<a href=\"example11.php\">Write a Review</a>
<a href=\"search.php\">Search</a>
<a href='logoutconfirmation.php'>Logout</a>
<hr />";
}else{
echo "<a href=\"index.php\">Home</a>
<a href=\"viewlist.php\">See Reviews</a>
<a href=\"example11.php\">Write a Review</a>
<a href=\"search.php\">Search</a>
<hr />";
}
} ?>
のindex.php
<?php
include "header.inc.php";
$pagetitle= "Login Form";
$showform =1;
$errormsg =0;
$errorusername = $errorpassword = "";
$inputdate = time();
//FIRST CHECK TO SEE IF THE USER IS LOGGED IN
if(isset($_SESSION['ID']))
{
echo "<p class='error'> You are already logged in. </p>";
include_once "footer.inc.php";
exit();
}
if(isset ($_POST['submit'])) {
/*************************************************************
* ALL FIELDS- STORE USER DATA; SANITIZE USER-ENTERED DATA
*************************************************************/
$formfield['username'] = trim($_POST['username']);
$formfield['password'] = trim($_POST['password']);
if (empty($formfield['username'])) {
$errorusername = "The username is required.";
$errormsg = 1;
}
if (empty($formfield['password'])) {
$errorpassword = "The password is required.";
$errormsg = 1;
}
if ($errormsg != 0) {
echo "<p class='error'> THERE ARE ERRORS!</p>";
} else {
//get the user data from the database
try {
$user = "SELECT * FROM Users WHERE username =:username";
$stmt = $pdo->prepare($user);
$stmt->bindValue(':username', $formfield['username']);
$stmt->execute();
$row = $stmt->fetch();
$countuser = $stmt->rowCount();
// if query okay, see if there is a result
if ($countuser < 1) {
echo "<p class='error'> *This user cannot be found in the
database* </p>";
} else {
if (password_verify($formfield['password'], $row['password'])) {
$_SESSION['ID'] = $row['ID'];
$showform = 0;
header("LocationL confirm.php?state=2");
echo "<p> Thank you for logging in! </p>";
} else {
echo "<p class='error'> The username and password
combinations you entered are not correct. Please try again! </p>";
}
}//username exists
} catch (PDOException $e) {
echo 'ERROR fetching users: ' . $e->getMessage();
exit();
}
}
}
if($showform == 1) {
?>
<p class="homemsg">Welcome to the Movie Review Hub! Feel free to look
around or sign in to write your own review.</p>
<form name="login" id="login" method="post" action="index.php">
<table class="center">
<tr>
<th><label for="username">Username: </label></th>
<td><input name="username" id="username" type="text" placeholder="Required Username"
}?><span class="error" <?php if (isset($errorusername)) {
echo $errorusername;
} ?></span></td>
</tr>
<tr>
<th><label for="password">Password: </label></th>
<td><input name="password" id="password" type="password" placeholder="Required Password"
}?><span class="error"> <?php if (isset($errorpassword)) {
echo $errorpassword;
} ?></span></td>
<tr>
<th><label for="submit">Submit: </label></th>
<td><input type="submit" name="submit" id="submit" value="submit"/></td>
</tr>
</table>
<p><a href=index.php>Register.</a></p>
<?php
include_once "footer.inc.php";
}
?>
私は誰かからログインする場合、私はすべてのページに表示するログアウトボタンをしたいと思います言ったようにインデックスページ、メニューはすべてのファイルに含まれています
最初にログアウトボタンログインボタンを押すと表示されますが、ページをリフレッシュするか、別のページに移動すると表示されなくなります。
あなたの 'session_start'呼び出しはどこですか? – CBroe
私はちょうどあなたに賞賛を与えたいと思います.PDOや準備されたクエリのような優れた方法を実際に使っている "非常に新しい"プログラマと、パスワードを扱うために 'password_verify'を使うことは非常にまれです。よくやった。 –