doulian5857
2016-12-31 19:31
浏览 8
已采纳

具有不同权限的mySQLi和PHP登录系统

I'm pretty new to PHP and I need to make a login system and I need to check if the user has certain permission, then show something on the page. Let me give a pseudo-code example:

IF USER HAS PERMISSION "CONTACTS" OR USER HAS PERMISSION "ALL"
  SHOW CONTACTS LINK

I've been using DevDojo's php login script, and I wanted something similar or an adaption of that code with a multi-level system. I've tried many things here on SO but none of them really fit my needs, I need to use something like if $user->hasPermission("contacts"). because as I have many features, levels like 1, 2 and etc. won't fit, and admin or not also won't. I'm pretty newbie, so explanation is welcome! Thanks in advance.

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • du718589 2017-01-01 07:40
    已采纳

    If you have 3 different levels of user with 3 being the master level...

    When the user logs in, set a PHP variable to the permission value set in the database.

    Create your template containing all visible content. Decide which sections will be hidden to users at each level and use PHP if statements to prevent blocks being loaded to the page.

    A very basic example would be

    <?php $level=2; ?>
    
    <img src="http://someimg.com/img.jpg" alt="level 1">
    
    <?php if($level >= 2){ ?>
     <img src="http://someimg.com/img.jpg" alt="level 2">
    <?php }
    
    if($level == 3){?>
     <img src="http://someimg.com/img.jpg" alt="level 3">
    <?php }?>
    

    In this example the variable $level represents the user level, this is followed by 3 images. The first image is available to all level users, the second image is available to user level 2 & above and the third is only visible to master level users. You can see the changes when you change "$level=2" to a value of 1 or 3.

    打赏 评论

相关推荐 更多相似问题