0

您好我正在创建一个基于用户级别的导航系统。我制作的代码没有显示任何错误,但没有显示菜单。我不知道我哪里出错了。我用谷歌搜索了这种菜单,但找不到任何解决方案。谁能对我犯的错误进行排序?或者告诉我是否有其他方法可以做到这一点?

这是我的代码。

    <html>
<head>
<title>Index</title>
</head>
    <body>

<!--database connection-->

<?php
//error_reporting(0);
'session_start()';
$con = new mysqli('localhost', 'username', 'password', 'database');
if($con->connect_errno > 0){
    die('Sorry, We\'re experiencing some connection problems.');
}
?>

<!--functions-->

<?php
    function loggedin(){
        if(isset($_SESSION['user_id'])){
            return true;
        }else{
            return false;
        }
    }
?>


<!--titlebar-->

<div>
    <?php
        if(loggedin()){
            $my_id=$_SESSION['user_id'];
            $log=$con->prepare("SELECT username,user_level FROM users WHERE user_id='$my_id'");
            $log->execute;
            $log->bind_result($username, $user_level,$my_id);
            $log->store_result;
            if($log->fetch()) //fetching the contents of the row
            {
                if($user_level=='a'){?>
                    <a href = 'index.php'>Home</a>
                    <a href = 'admin.php'>Admin</a>
                    <a href = 'index.php'>Log Out</a>
                <?php
                }if($user_level=='m'){?>
                    <a href = 'index.php'>Home</a>
                    <a href = 'profile.php'>Profile</a>
                    <a href = 'index.php'>Log Out</a>
                <?php
                }else{?>
                    <a href = 'index.php'>Home</a>
                    <a href = 'login.php'>Login</a>
                    <a href = 'register.php'>Register</a>
            <?php
            }
        }
        }
    ?>

</div>
Index
</body>
</html>

我对我的登录页面使用了相同的方法,效果很好。这是我的登录页面代码。

<html>
<head>
<title>LOGIN</title>
</head>
<body>
<?php include 'connect.php';?>
<?php include 'functions.php';?>
<?php include 'titlebar.php';?>

<h3>LOGIN HERE:</h3>
<form action ="" method="post">

User Name:<br/>
<input type='text' name='username' />
<br/><br/>
Password:<br/>
<input type='password' name='password' />
<br/><br/>
<input type='submit' name='submit' value='login'>
</form>
<?php

if(isset($_POST['submit'])){
    $username = $_POST['username'];
    $password = md5($_POST['password']);


    $stmt = $con->prepare("SELECT user_id, username, password, status FROM users WHERE username=? AND password=? LIMIT 1");
    $stmt->bind_param('ss', $username, $password);
    $stmt->execute();
    $stmt->bind_result($user_id, $username, $password, $status);
    $stmt->store_result();
    if($stmt->num_rows == 1)  //To check if the row exists
        {
            if($stmt->fetch()) //fetching the contents of the row
            {
               if ($status == 'd') {
                   echo "YOUR account has been DEACTIVATED.";
                   exit();
               } 
               if ($status == 'b') {
                   echo "YOUR account has been BANNED.";
                   exit();
               } 
               if ($status == 'n') {
                   echo "YOUR account has NOT YET BEEN ACTIVATED.";
                   exit();
               } 
               else {
                   $_SESSION['Logged'] = 1;
                   $_SESSION['user_id'] = $user_id;
                   $_SESSION['username'] = $username;
                   echo 'Success!';
                   exit();
               }
           }

    }
    else {
        echo "INVALID USERNAME/PASSWORD Combination!";
    }
    $stmt->close();
}
else 
{   

}
$con->close();

?>
</body>
</html>

任何帮助将不胜感激。

谢谢你。

4

3 回答 3

0

好的。以下是我对您的问题的理解:您需要在导航中根据用户级别显示一定数量的链接:如果管理员已登录,则应显示这些链接,或者如果前台已登录,则应显示这些链接。

如果是这样,那么这就是你的做法,而不是我的做法:

<?php
//Start session
    session_start();

    //Check whether the session variable SESS_MEMBER_ID is present or not
    if(!isset($_SESSION['SESS_MEMBER_ID']) || (trim($_SESSION['SESS_MEMBER_ID']) == '')) {
        header("location: index.php");
        exit();
    } 

if($_SESSION['SESS_FIRST_NAME']=="admin"){

 /*Your Links go here*/

 }

if($_SESSION['SESS_FIRST_NAME']=="frontdesk"){

/*Your links go here*/

}?>

这是我的 login.php

<?php
    //Start session
    session_start();

    //Connect to mysql server
    /* Database connection goes here*/

    //Function to sanitize values received from the form. Prevents SQL injection
    function clean($str) {
        $str = @trim($str);
        if(get_magic_quotes_gpc()) {
            $str = stripslashes($str);
        }
        return mysql_real_escape_string($str);
    }

    //Sanitize the POST values
    $login = clean($_POST['user']);
    $password = clean($_POST['password']);

    //Create query
    $qry="SELECT * FROM user WHERE username='$login' AND password='$password'";
    $result=mysql_query($qry);

    //Check whether the query was successful or not
    if($result) {
        if(mysql_num_rows($result) > 0) {
            session_regenerate_id();
            $member = mysql_fetch_assoc($result);
            $_SESSION['SESS_MEMBER_ID'] = $member['user_id'];
            $_SESSION['SESS_FIRST_NAME'] = $member['position'];
            session_write_close();
            header("location: home_admin.php");
            exit();

        }else {
            //Login failed
            header("location: admin_index.php");
            exit();
        }
    }else {
        die("Query failed");
    }
?>
于 2013-07-05T08:49:28.363 回答
0

所以,login() 函数似乎不起作用

  1. 您已经登录了吗?
  2. '会话开始'; ===== > 你在这里提到过 session_start() 吗?(如果是这样,请不要忘记在任何输出之前开始您的会话。例如,就在使用“html”标签之前)

升级版:

  1. 您应该已登录。
  2. 你必须开始你的会话
  3. 处理 mysqli 对象和语句还有一些其他问题。

我已编辑此代码以使其正常工作并在其中添加一些注释。看看这里

http://pastebin.com/Za1V0VES

于 2013-07-05T07:33:27.110 回答
0

尝试使用回声

<!--titlebar-->
<div>
<?php
    if(loggedin()){
        $my_id=$_SESSION['user_id'];
        $log=$con->prepare("SELECT username,user_level FROM users WHERE user_id='$my_id'");
        $log->execute;
        $log->bind_result($username, $user_level,$my_id);
        $log->store_result;
        if($log->fetch()) //fetching the contents of the row
        {
            if($user_level=='a'){
                echo "
                 <a href = 'index.php'>Home</a>
                 <a href = 'admin.php'>Admin</a>
                 <a href = 'index.php'>Log Out</a>
                ";
            }if($user_level=='m'){
                echo "
                 <a href = 'index.php'>Home</a>
                 <a href = 'profile.php'>Profile</a>
                 <a href = 'index.php'>Log Out</a>
                ";
            }else{
                echo "
                 <a href = 'index.php'>Home</a>
                 <a href = 'login.php'>Login</a>
                 <a href = 'register.php'>Register</a>
                ";
            }
        }
    }
?>

于 2016-12-07T23:31:17.173 回答