Php 更改页脚以在用户登录网站时将登录标记为注销

Php 更改页脚以在用户登录网站时将登录标记为注销,php,login,Php,Login,我需要一些帮助,让我的页脚登录改为注销,如果用户登录。我在login.php页面上找到了它,当用户登录时,它会标记为logout,但如果我转到About页面,它仍然会显示login。在我的页脚里我有 <?php if(isset($_SESSION['user'])) { print("<a href=\"logout.php\">Log Out</a>"); }

我需要一些帮助,让我的页脚登录改为注销,如果用户登录。我在login.php页面上找到了它,当用户登录时,它会标记为logout,但如果我转到About页面,它仍然会显示login。在我的页脚里我有

<?php
                if(isset($_SESSION['user'])) {
                    print("<a href=\"logout.php\">Log Out</a>");
                }
                else{
                 print("<a href=\"login.php\">Login</a>");
                }
            ?>

但它似乎不起作用,只是在登录页面上。我将在下面发布关于、登录和页脚页面的代码。我一定是做了一些错误的会议,因为它只在一页工作,但我似乎找不到地方。抱歉,代码太多了,但我想全部粘贴,以防我没有粘贴发生问题的部分

多谢各位

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>


session\u start()。(2) 看起来您在登录页面中取消/销毁了会话->
包括'footer.php';session_unset();会话_destroy()您在header.php文件或其他任何地方使用过session_start()吗?+1@sean表示第(2)点。。。为什么要在login.php的底部销毁会话?好吧,那就去掉login.php底部的unset和destroy?然后在每个页面上添加sessionstart?好的,去掉login.php底部的unset和destroy?并将会话开始添加到每页的顶部?@Terry yup应该可以。试试看问题是否解决了
<?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>
<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>