简体   繁体   English

PHP限制用户内容

[英]php restrict user content

So I'm trying to make a dummy website where only members who are marked as premium can see certain content. 因此,我正在尝试制作一个虚拟网站,只有标记为高级的会员才能看到某些内容。 Since it's a dummy website the way this is distinguished is by selecting yes or no on a checkbox while creating an account. 由于这是一个虚拟网站,因此可以通过在创建帐户时在复选框上选择是或否来区分。 But, what I'm having trouble figuring out is how I can have the entire site check to make sure the user is a premium member and not a normal member or guest. 但是,我要弄清楚的是如何进行整个站点检查,以确保用户是高级会员,而不是普通会员或访客。 On the registration page itself, I figured I could use an if statement to initially mark it like this 在注册页面本身上,我认为可以使用if语句将其最初标记为这样

if($user['premium'] == 1){
   $_SESSION['premium'] = true;
}

but how would I make it so a page like membersonly.php ONLY shows up if the user is a premium member? 但是,如果用户是高级会员,那么如何显示members.php这样的页面呢? I'm assuming I'd have to use either a function or a class but I truly do not know what I would need to do inside either. 我假设我必须使用一个函数或一个类,但是我真的不知道在这两个函数中我需要做什么。

EDIT I guess I didn't explain myself clearly enough, not surprising since I posted this at 2am. 编辑我想我对自己的解释不够清楚,这并不奇怪,因为我在凌晨2点发布了此信息。 I don't want to just redirect normal users and guests away from membersonly.php I want it so the link for the membersonly page ONLY shows up on the nav bar if the user has a premium account. 我不想只是将普通用户和访客重定向到远离membersonly.php,因此,如果用户具有高级帐户,则仅在导航栏上显示membersonly页面的链接。 Could this be accomplished with an if statement or could I need to create a function or class dedicated to monitoring this? 可以使用if语句完成此操作,还是需要创建一个专用于监视此功能的函数或类?

You can simply do by the following steps: 1. On the button click post event first check if the user is premium or normal 2. Put an if condition that if user is premium then 您只需执行以下步骤即可:1.在按钮上单击发布事件,首先检查用户是否为高级用户或普通用户。2.设置if条件,如果用户为高级用户,则

header('Location: ****.php'); 

else to the other php file using the same way. 其他使用相同的方式到另一个PHP文件。

NOTE: you need to add to you database if the user is premium or normal. 注意:如果用户是高级或普通用户,则需要添加到数据库中。

I assume you know basic php follow just below steps on your membership page. 我假设您知道基本的php遵循会员页面上的以下步骤。

  1. Create connection to database. 创建与数据库的连接。

  2. Fetch user from user id (I think you done use login for it). 从用户ID提取用户(我认为您已经使用登录名了)。

  3. check condition where user is premium or not if user is not premium then redirect it to home page. 检查用户是否为高级用户的条件,如果用户不是高级用户,则将其重定向到主页。

    require_once('connection.php'); // Fetch user details. if($user['premium'] != 1){ header("location:index.php"); }

Add a flag column in your db table 'is_premium'.Upon saving, if a user is premium than insert 1 in this flag else 0.Than create a function which checks if logged in user is premium or not and call this function on start of every page.following is the implementation of function which checks for premium user. 在数据库表'is_premium'中添加一个标志列,保存后,如果用户是高级用户,则在此标志中插入1,否则为0。 every page.following是检查高级用户的功能的实现。

 function checkPremium(){
 if($_SESSION['premium'] == true){
    return true;
 }else{
  header('Location:some_other_page.php');
 }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM