嗨,我正在创建一个基于用户级别的导航系统。我写的代码没有显示任何错误,但它没有显示菜单。我不知道我哪里错了。我在谷歌上搜索了这种菜单,但我找不到任何解决方案。有人能纠正我所犯的错误吗?或者告诉我有没有其他方法?
这是我的代码。
<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>任何帮助都会得到重视。
谢谢。
发布于 2013-07-05 15:33:27
因此,看起来loggedin()函数不起作用
你已经在这里登录了吗?
更新:
我对这段代码进行了编辑,使其正常工作,并在其中添加了一些注释。请看这里
发布于 2013-07-05 16:49:28
好吧。我是这样理解你的问题的:你需要在你的导航中根据用户级别显示一定数量的链接:如果管理员登录,那么这些链接应该显示,或者如果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");
}
?>发布于 2016-12-08 07:31:17
尝试使用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>
";
}
}
}
?>
https://stackoverflow.com/questions/17483328
复制相似问题