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($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? 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. 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. Could this be accomplished with an if statement or could I need to create a function or class dedicated to monitoring this?

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

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

else to the other php file using the same way.

NOTE: you need to add to you database if the user is premium or normal.

no need of class and function.

I assume you know basic php follow just below steps on your membership page.

  1. Create connection to database.

  2. Fetch user from user id (I think you done use login for it).

  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.

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