您好我正在创建一个基于用户级别的导航系统。我做的代码没有显示任何错误,但它没有显示菜单。我不知道我哪里出错了。我用谷歌搜索这种菜单,但我找不到任何解决方案。任何人都可以解决我犯的错误吗?或者告诉我是否有其他方法可以做到这一点?
这是我的代码。
<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>
任何帮助都会受到欢迎。
谢谢。
答案 0 :(得分:0)
因此,看起来loginin()函数不起作用
UPD:
我已编辑此代码以使其正常工作并对其进行一些注释。 看看这里
答案 1 :(得分:0)
好。以下是我理解您的问题的方法:您需要在导航中根据用户级别显示一定数量的链接:如果管理员已登录,则应显示这些链接或者如果记录了frontdesk然后应显示那些链接。
如果是这样的话,那你就是这样做的,而不是我是怎么做到的:
<?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");
}
?>
答案 2 :(得分:0)
尝试使用echo
<!--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>
";
}
}
}
?>