0

这就是我所拥有的 headers.php 无论如何它应该是我的导航栏。这是问题所在,当我以会员身份以用户身份登录时,整个标题都不会出现。但是当我用管理员登录时,导航会“神奇地”出来!

    <?php
        if(!isset($_SESSION['sRole'])){
        ?>
            <div id="header">
            <div id="fb-root"></div>
                 <div id ="inthebox">
                        <a href="login.php" class="link"><b>LOGIN</b></a>|
                        <a href="register.php" class="link"><b>REGISTER</b></a>
                </div>  
                        <div id ="outthebox">
                            <a href="index.php" class="link">HOME</a>|
                            <a href="bookshelf.php" class="link">BOOKSHELF</a>|
                            <a href="shoppingcart.php" class="link">SHOPPING CART</a>|
                            <a href="about.php" class="link">ABOUT</a>|
                                                <a href="logout.php" class="link">ABOUT</a>|

                        </div>




            </div>

        <?php
        }
        else{

                if($_SESSION['sRole'] == "member"){
        ?>
        <div id="header">
                                <div id ="inthebox">
                        <a href="logout.php" class="link"><b>LOGOUT</b></a>
                                        </div>

                                     <div id ="outthebox">
                            <a href="index.php" class="link">HOME</a>|
                            <a href="bookshelf.php" class="link">BOOKSHELF</a>|
                            <a href="shoppingcart.php" class="link">SHOPPING CART</a>|
                            <a href="about.php" class="link">ABOUT</a>|
                                                <a href="updateProfile.php" class="link">PROFILE</a>
                            <?php
                                echo("You have Login as :" . $_SESSION['sUsername']);
                            ?>
                        </div> 





            </div>
        <?php
            }else{
                if($_SESSION['sRole']=="admin"){
                    ?>
            <div id="header">
                    <div id ="inthebox">
                    <a href="logout.php" class="link"><b>LOGOUT</b></a>
                        </div>
                    <div id="outthebox">
                            <a href="index.php" class="link">HOME</a>|
                            <a href="bookshelf.php" class="link">BOOKSHELF</a>|
                            <a href="shoppingcart.php" class="link">SHOPPING CART</a>|
                            <a href="about.php" class="link">ABOUT</a>|
                                                <a href="account.php" class="link">Manage Account</a>|
                                                <a href="managebook.php" class="link">Manage Books</a>|
                                                <a href="manageOrder.php" class="link">Manage Orders</a>|
                            <?php
                                echo("You have Login as :" . $_SESSION['sUsername']);
                            ?>
                        </div> 




            </div>
                    <?php
                }
            }
        }   
        ?>

这是我的 doLogin.php 页面,也许它可以帮助这里的任何人解决这个问题。我将 id、用户名、名字和姓氏存储到会话中。里面有 alr 。我进去时的网站hor是没有错误的。没有html代码错误或任何东西。只是它没有出现。然而,导航链接下方的文字仍然会出现。

    <?php
     //connect to database
    include ('dbfunction.php'); 
    if (!isset($_POST['Login'])) {
        if (isset($_POST['username'])) {
            //retrieve form data
            $username = $_POST['username'];
            $password = $_POST['password'];

            $query = "SELECT * FROM users WHERE username='" . $username . "'AND password = '" . $password . "'";
            $result = mysql_query($query) or die('The error :' . mysql_error());
            $num_rows =mysql_num_rows($result);

            if($num_rows == 0){
                 header('Location:login.php');
                exit();
                }
            //if record is found, store id and username into session
            else{
                $row = mysql_fetch_array($result);
                $_SESSION['sUsername'] = $row['username'];
                $_SESSION['sRole'] = $row['role'];
                $_SESSION['sFirst_name'] = $row['first_name'];
                $_SESSION['sLast_name'] = $row['last_name'];

                header('location:successful_login.php');//redirect to this page
                exit();
            } 
        }
            else { 


            }
    } else {
            header('Location:successful_login.php');
            exit();
    }
    mysql_close();
    ?>
4

2 回答 2

0

我认为这里的问题是您正在使用} else {条件,但随后if在其中嵌套了一条语句。事实上,看起来你的右大括号坏了。这是修改后的代码:

<?php if(!isset($_SESSION['sRole'])){ ?>
    <div id="header">
    <div id="fb-root"></div>
         <div id ="inthebox">
                <a href="login.php" class="link"><b>LOGIN</b></a>|
                <a href="register.php" class="link"><b>REGISTER</b></a>
        </div>  
                <div id ="outthebox">
                    <a href="index.php" class="link">HOME</a>|
                    <a href="bookshelf.php" class="link">BOOKSHELF</a>|
                    <a href="shoppingcart.php" class="link">SHOPPING CART</a>|
                    <a href="about.php" class="link">ABOUT</a>|
                                        <a href="logout.php" class="link">ABOUT</a>|

                </div>




    </div>

<?php } else if($_SESSION['sRole'] == "member") { ?>

<div id="header">
                        <div id ="inthebox">
                <a href="logout.php" class="link"><b>LOGOUT</b></a>
                                </div>

                             <div id ="outthebox">
                    <a href="index.php" class="link">HOME</a>|
                    <a href="bookshelf.php" class="link">BOOKSHELF</a>|
                    <a href="shoppingcart.php" class="link">SHOPPING CART</a>|
                    <a href="about.php" class="link">ABOUT</a>|
                                        <a href="updateProfile.php" class="link">PROFILE</a>
                    <?php
                        echo("You have Login as :" . $_SESSION['sUsername']);
                    ?>
                </div> 





    </div>

<?php }else if($_SESSION['sRole']=="admin"){ ?>

    <div id="header">
            <div id ="inthebox">
            <a href="logout.php" class="link"><b>LOGOUT</b></a>
                </div>
            <div id="outthebox">
                    <a href="index.php" class="link">HOME</a>|
                    <a href="bookshelf.php" class="link">BOOKSHELF</a>|
                    <a href="shoppingcart.php" class="link">SHOPPING CART</a>|
                    <a href="about.php" class="link">ABOUT</a>|
                                        <a href="account.php" class="link">Manage Account</a>|
                                        <a href="managebook.php" class="link">Manage Books</a>|
                                        <a href="manageOrder.php" class="link">Manage Orders</a>|
                    <?php
                        echo("You have Login as :" . $_SESSION['sUsername']);
                    ?>
                </div> 




    </div>
            <?php
}  
于 2013-07-23T02:29:23.797 回答
0

尝试这个:

<?php if( (!isset($_SESSION['sRole'])) || (empty($_SESSION['sRole'])) || (is_null($_SESSION['sRole'])) ): ?>
 <html>your code</html>
<?php else: ?>
      <?php switch($_SESSION['sRole']) {
            case 'admin': { // admin code } break;
            case 'member': { // member code } break;
            default: { // something happened } break;
       }
      ?>
<?php endif; ?>

检查并诊断问题

于 2013-07-23T03:01:08.113 回答