사용자가 로그인 및 로그 아웃하고 다른 기본 기능을 사용하는 간단한 웹 사이트 코드를 작성하고 있습니다. 누구든지 로그인 할 때 로그 아웃 버튼이 방문하는 모든 페이지에 표시되고 로그되지 않은 경우 숨겨지기를 바랍니다. 나는 아직도 새롭고 잘못된 점을 알아 내지 못합니다. 로그인 버튼을 처음 클릭하자마자 로그 아웃 버튼이 나타나지만 다른 페이지로 이동하면 버튼이 내 메뉴에서 사라집니다.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 '를 사용하는 "매우 새로운"프로그래머를 보는 것은 거의 없습니다. 잘 했어. –