如果用户已登录,我需要一些帮助才能在我的页脚中登录以更改为注销。我在login.php页面上工作,当用户登录时,它会标记注销,但如果我转到关于页面,然后它仍然说登录。在我的页脚中我有
<?php
if(isset($_SESSION['user'])) {
print("<a href=\"logout.php\">Log Out</a>");
}
else{
print("<a href=\"login.php\">Login</a>");
}
?>
但它似乎不起作用,只在登录页面上。我将在下面发布about,login和footer页面的代码。我必须对会话做一些错误,因为它只在一页上工作,但我似乎无法找到它。很抱歉它有很多代码,但我想把它全部粘贴,以防我没有粘贴发生问题的部分
非常感谢
的login.php
<?php
include "db.php";
if (isset($_POST['username']) && isset($_POST['password']))
{
$loginQuery = mysqli_query($con, "SELECT * FROM `users` WHERE `hashpassword` = '".sha1($_POST['password'])."' AND `username` = '".$_POST['username']."'");
if (mysqli_num_rows($loginQuery) == 1) $_SESSION['user'] = $_POST['username'];
}
?>
<?php
$pageName = "Administrator Login";
include 'header.php';
?>
<div class="one">
<?php
if(isset($_SESSION['user'])) {
echo "Welcome! You are logged in as : ".$_SESSION['user'];
//adding photos
print("<br/>Add A Photo");
$mysqli = new mysqli(DB_HOST,DB_USER, DB_PASSWORD, DB_NAME);
$result = $mysqli->query("SELECT DISTINCT photoYear FROM Photos");
print('<form method="post" enctype="multipart/form-data"><table>
<tr><td>*Upload Image</td><td><input type="file" name="newphoto"/></td></tr>
<tr><td>*Year To Upload To</td><td><input type="text" name="year"></td></tr><tr><td></td><td class="left"><input type="submit" name="sub" value="Add Photo" /></td></tr></table></form>');
}
else {
?><h1>Please Login Here To Make Administrative Changes</h1>
<form action="" method="post">
Username: <input type="text" name="username"> <br>
Password: <input type="password" name="password"> <br>
<input type="submit" value="Log In">
</form>
<?php } ?>
<?php
/* creating new year albums and uploading photos
if(isset($_SESSION['user'])){
//once user is logged in... can upload photos to certain years
/* Adding photo to year albums
print('<form method="post" enctype="multipart/form-data"><table>
<tr><td>*Upload Image</td><td class="left" ><input type="file" name="newphoto"/></td></tr>
<tr><td>*Year To Upload To</td><td class="left" ><select name="yearz">');
while($row = $result -> fetch_assoc()){
$year=$row[ 'photoYear'];
print("<option value=".$year.">".$year."</option>");
}
print('</select></td></tr><tr><td></td><td class="left"><input type="submit" name="sub" value="Add Photo" /></td></tr></table></form>');
if (!empty($_FILES[ 'newphoto' ]) && isset($_POST['yearz']) && isset($_POST['sub'])){
$newPhoto = $_FILES[ 'newphoto' ];
$year = $_POST['yearz'];
$errors= array();
$maxsize = 2097152;
$acceptable = array(
'application/pdf',
'image/jpeg',
'image/jpg',
'image/gif',
'image/png'
);
if(($_FILES['newphoto']['size'] >= $maxsize) || ($_FILES["newphoto"]["size"] == 0)) {
$errors[] = 'File too large. File must be less than 2 megabytes.';
}
if(!in_array($_FILES['newphoto']['type'], $acceptable) && (!empty($_FILES['newphoto']['type']))) {
$errors[] = 'Invalid file type. Only PDF, JPG, GIF and PNG types are accepted.';
}
//next
if(count($errors) ===0){
$originalName = str_replace(" ","_",$newPhoto[ 'name' ]);
$result = $mysqli->query("SELECT photoURL FROM Photos WHERE photoURL =".$originalName);
if (!($row = $result -> fetch_assoc())){
$test = explode('|', $_POST['alb']);
$albumid = $test[0];
$caption = $_POST[ 'caption' ];
if ($newPhoto['error'] == 0){
$tempName = $newPhoto[ 'tmp_name' ];
move_uploaded_file( $tempName, "$originalName");
$_SESSION[ 'photos'][] = $originalName;
print("File was uploaded.");
$adding = "INSERT INTO Photos (`photoURL`,`photoYear`)
VALUES('$originalName', '$yearz');";
$mysqli -> query($adding);
}
}
else{
print("This photo is already in the database. Try uploading another.");
}
}
else{
foreach($errors as $error) {
echo '<script>alert("'.$error.'");</script>';
}
die();
}
}
//outer
}
*/
?>
</div>
<?php
include "db.php";
include 'footer.php';
session_unset();
session_destroy();
?>
</html>
about.php
<?php
$pageName = "About RFYC";
include 'header.php';
?>
<div class="one">
<img class="center" src="images/logo/black.png" alt="logo">
<p>
Root For Your Cause is an annual charity root beer pong tournament held at Cornell University. The event is sponsored by Alpha Phi Omega - Gamma Chapter, a national community service fraternity. When registering for the event each team or organization selects a charity to play for. At the end of this single-elimination tournament, the entire sum of money earned through registration is then donated to the winning team's charity of choice!<br/>
</p>
<p>
The event is also friendly to spectators. There is music, plenty of chairs to watch friends compete, and we sell root beer floats throughout the competition.
The registration fee is $10 per two-person team for individual teams, and $8 per two-person team for organizations registering 5 or more teams who will all be competing for the same charity.
For more information, contact <span id="email">philanthropy@apogamma.org</span>
</p>
</div>
<div class="winners">
<h1>Past Winners</h1>
<figure>
<figcaption>2011<br/>
<img src="images/icons/placeholder_circle.png" alt="winner"><br/>
Team Name<br/>Charity</figcaption>
</figure>
<figure>
<figcaption>2012<br/>
<img src="images/icons/placeholder_circle.png" alt="winner">
<br/>Team Name<br/>Charity</figcaption>
</figure>
<figure>
<figcaption>2013<br/>
<img src="images/icons/placeholder_circle.png" alt="winner">
<br/>Team Name<br/>Charity</figcaption>
</figure>
</div>
<?php
include 'footer.php';
?>
</html>
footer.php
<div id="wrapper">
<div id="footer">
<div class="footerFloat">
<h4>Site Map</h4>
<a href="about.php">About</a> <br/>
<a href="rules.php">Rules</a><br/>
<a href="gallery.php">Gallery</a><br/>
<a href="bracket.php">Bracket</a><br/>
<a href="register.php">Register</a>
</div>
<div class="footerFloat">
<h4>Connect With Us</h4>
<ul>
<li><img src="images/icons/email.png" alt="email"></li>
<li><img src="images/icons/fb.png" alt="fb"></li>
<li><img src="images/icons/twitter.png" alt="twitter" ></li>
</ul>
</div>
<div class="footerFloat">
<h4>Manage</h4>
<?php
if(isset($_SESSION['user'])) {
print("<a href=\"logout.php\">Log Out</a>");
}
else{
print("<a href=\"login.php\">Login</a>");
}
?>
</div>
<div class="footerFloat">
<h4>Brought to You By</h4>
<a href="http://apogamma.org/">Alpha Phi Omega - Cornell Chapter </a>
</div>
</div>
</div>
</body>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script src="js/interactivity.js" type="text/javascript"></script>
答案 0 :(得分:2)
session_start()
需要包含在所有网页上,否则您的会话将不存在。