我知道我的PHP代码有什么问题吗?

时间:2013-07-23 02:17:30

标签: php

这就是我对headers.php所拥有的,无论如何它被认为是我的导航栏。这是问题,当我以一个用户身份登录成员时,整个标题都没有出来。但是当我使用admin登录时,导航将“神奇地”出来!

    <?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。我进去的网站是没有错误的网站。没有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();
    ?>

2 个答案:

答案 0 :(得分: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
}  

答案 1 :(得分: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; ?>

检查并诊断问题