我有这个PHP代码:
<nav><ul>
<?php
if( (isset($_SESSION['user_role']) ) && (false != $_SESSION['user_role']) )
{
if( '2' == $_SESSION['user_role'] )
{
echo "<li><a href='index.php'>Home</a></li>";
echo "<li><a href='#'>About Us</a></li>";
echo "<li><a href='#'>Contact Us</a></li>";
echo "<li><a href='loginpage.php'>Login</a></li>";
}
elseif ('3' == $_SESSION['user_role'] )
{
echo "<li><a href='index.php'>Home</a></li>";
echo "<li><a href='#'>About Us</a></li>";
echo "<li><a href='#'>Contact Us</a></li>";
echo "<li><a href='loginpage.php'>Login</a></li>";
echo "<li><a href='#'>Admin</a></li>";
}
}
?>
<ul></nav>
出于某种原因,当我在浏览器上运行php文件时,正在回显的html代码没有显示。
有什么建议吗?
这是我设置会话变量的地方:
$con = mysqli_connect("localhost","root","","darrenvellaedp2") or die("Error: " . mysqli_error($con));
$result = "SELECT userRoleID FROM tbl_users WHERE userRoleID = 2" or die("Error: " . mysqli_error($con));
$res = $con->query($result);
while($row = mysqli_fetch_array($res)) {
$UserRoleID = $row['userRoleID'];
$_SESSION['user_role'] = $UserRoleID;
}
答案 0 :(得分:4)
如果没有生成输出,则表示您的第一个条件IF
语句失败。即:
if( (isset($_SESSION['user_role']) ) && (false != $_SESSION['user_role']) )
您应该添加var_dump($_SESSION['user_role'])
以检查会话数据是否存在。
P.S。你还记得在这个剧本的开头添加session_start();
吗?即..
<?php
// start php session
session_start();
?>
<nav><ul>
// rest of your code
答案 1 :(得分:0)
试试这个:
第一页
<?php
session_start();
$con = mysqli_connect("localhost","root","","darrenvellaedp2") or die("Error: " . mysqli_error($con));
$result = "SELECT userRoleID FROM tbl_users WHERE userRoleID = 2" or die("Error: " . mysqli_error($con));
$res = $con->query($result);
while($row = mysqli_fetch_array($res)) {
$UserRoleID = $row['userRoleID'];
$_SESSION['user_role'] = $UserRoleID;
}
?>
第二页
<?php
session_start();
?>
<nav><ul>
<?php
if( (isset($_SESSION['user_role']) ) && (false != $_SESSION['user_role']) )
{
if( '2' == $_SESSION['user_role'] )
{
echo "<li><a href='index.php'>Home</a></li>";
echo "<li><a href='#'>About Us</a></li>";
echo "<li><a href='#'>Contact Us</a></li>";
echo "<li><a href='loginpage.php'>Login</a></li>";
}
elseif ('3' == $_SESSION['user_role'] )
{
echo "<li><a href='index.php'>Home</a></li>";
echo "<li><a href='#'>About Us</a></li>";
echo "<li><a href='#'>Contact Us</a></li>";
echo "<li><a href='loginpage.php'>Login</a></li>";
echo "<li><a href='#'>Admin</a></li>";
}
}
?>
</ul></nav>
答案 2 :(得分:0)
接下来,将条件session_status放在您共享的第一个代码段中:
<?php
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
$con = mysqli_connect("localhost","root","","darrenvellaedp2") or die("Error: " . mysqli_error($con));
$sql = "SELECT userRoleID FROM tbl_users WHERE userRoleID = 2";
$res = $con->query($sql) or die("Error: " . mysqli_error($con));
$row = mysqli_fetch_array($res);
$_SESSION['user_role'] = $row['userRoleID'];
?>
第二个print_r
:
<nav><ul>
<?php
print 'user_role: ';
print_r($_SESSION['user_role']);
if( (isset($_SESSION['user_role']) ) && (false != $_SESSION['user_role']) )
{
if( '2' == $_SESSION['user_role'] )
{
echo "<li><a href='index.php'>Home</a></li>";
echo "<li><a href='#'>About Us</a></li>";
echo "<li><a href='#'>Contact Us</a></li>";
echo "<li><a href='loginpage.php'>Login</a></li>";
}
elseif ('3' == $_SESSION['user_role'] )
{
echo "<li><a href='index.php'>Home</a></li>";
echo "<li><a href='#'>About Us</a></li>";
echo "<li><a href='#'>Contact Us</a></li>";
echo "<li><a href='loginpage.php'>Login</a></li>";
echo "<li><a href='#'>Admin</a></li>";
}
}
?>
</ul></nav>
并报告user_role:
输出的内容。