PHP检查用户是否是管理员/用户并显示按钮

我不是PHP专业人士,因此需要一些帮助。

我在我的网站上有一个登录功能,该功能可以检查用户是管理员还是普通用户,并且工作正常。

我添加了一项新功能,系统应在该功能中检查用户是否已登录并显示不同的按钮。

  • 如果用户未登录,则显示登录/注册按钮。
  • 如果用户以用户身份登录,则显示user.php按钮和
       logout.php
  • 如果用户是管理员,则显示admin.php和logout.php

但是,它仅对用户有效。我想念什么? UserTypeID在数据库中为用户设置了1个,为管理员设置了2个。

session_start();
<?php
if(isset($_SESSION['UserTypeID']) == 1) //check if user is a user and display buttons
{
?>
    <li><a href="user.php">My account</a></li>
    <li><a href="logout.php">Logout</a></li>

<?php } elseif(isset($_SESSION['UserTypeID']) == 2) //check if user is an admin and display buttons
{
?>
    <li><a href="admin.php">My account</a></li>
    <li><a href="logout.php">Logout</a></li>

<?php  }else{ // if user is not logged in then display these buttons?> 
    <li><a href="signin.php">Sign In</a></li>
    <li><a href="signup.php">Sign Up</a></li>
<?php } ?>


qq353883454 回答:PHP检查用户是否是管理员/用户并显示按钮

做这样的事情。这会起作用。

<?php 
session_start();
if(!empty($_SESSION['UserTypeID']) && $_SESSION['UserTypeID']== 1) //check if user is a user and display buttons
{
?>
<li><a href="user.php">My Account</a></li>
<li><a href="logout.php">Logout</a></li>

<?php } else if(!empty($_SESSION['UserTypeID']) && $_SESSION['UserTypeID']== 2) //check if user is an admin and display buttons
{
?>
<li><a href="admin.php">My Account</a></li>
<li><a href="logout.php">Logout</a></li>

<?php  }else{ // if user is not logged in then display these buttons?> 
<li><a href="signin.php">Sign In</a></li>
<li><a href="signup.php">Sign Up</a></li>

<?php } ?>
,

isset($_SESSION['UserTypeID'])返回truefalse

session_start();
<?php if(isset($_SESSION['UserTypeID']) && $_SESSION['UserTypeID']== 1) //check if user is a user and display buttons
    {
    ?>
    <li><a href="user.php">My Account</a></li>
    <li><a href="logout.php">Logout</a></li>

    <?php } elseif(isset($_SESSION['UserTypeID']) && $_SESSION['UserTypeID'] == 2) //check if user is an admin and display buttons
    {
    ?>
    <li><a href="admin.php">My Account</a></li>
    <li><a href="logout.php">Logout</a></li>


<?php  }else{ // if user is not logged in then display these buttons?> 
<li><a href="signin.php">Sign In</a></li>
<li><a href="signup.php">Sign Up</a></li>
<?php } ?>
,

session_start()函数需要在php内编写并编写如下代码:

  <?php 
    session_start();
    if(isset($_SESSION['UserTypeID']) && $_SESSION['UserTypeID']== 1) //check if user is a user and display buttons
     {
     ?>
   <li><a href="user.php">My Account</a></li>
   <li><a href="logout.php">Logout</a></li>

  <?php } elseif(isset($_SESSION['UserTypeID']) && $_SESSION['UserTypeID'] == 2) //check if user is an admin and display buttons
       {
      ?>
     <li><a href="admin.php">My Account</a></li>
   <li><a href="logout.php">Logout</a></li>


   <?php  }else{ // if user is not logged in then display these buttons?> 
  <li><a href="signin.php">Sign In</a></li>
  <li><a href="signup.php">Sign Up</a></li>
 <?php } ?>
本文链接:https://www.f2er.com/3022802.html

大家都在问