The database work involves two steps:
Add a user level column to your existing user table. It would be convenient to just call it "user_level". Its type could be INT with a length of 2.
Assign user level to users. You need to work out a scheme to decide what levels users are given. I would say most users are given lowest level (e.g. 0 or 1) by default when they register. When they register, in addition to their user login and personal info (if any), a user level number is also inserted to the database table. If you want to change their user levels for some reasons (admin or paid services, etc), you can create an interface to update the user_level field.
User level check on each page also involves two stepd:
- When a user is logged in, his/her user_level is stored in a session. Log in script normally takes the input username and password, and compares them with those selected from user table in database. While selecting user information from database, also take the user_level data, and store it in a session. For example,
<?
$query = "SELECT * FROM usertable WHERE username='$username]'";
$query_result = mysql_query ($query) or die (mysql_error());
$result = @mysql_fetch_array ($query_result);
if ($result[password]!="")//make suer it is not blank
{
if ($password == $result[password]) // if passwords match
{
// If a session already exist, destroy it
if(isset($_COOKIE[session_name()]))
{
session_start(); // To be able to use session_destroy
session_destroy(); // To delete the old session file
unset($_COOKIE[session_name()]);
}
session_start();//start a new session
$SESSION[username]=$result[username];
$SESSION[user_level]=$result[user_level];//that's what you want
}
}
?>
$_SESSION[user_level] will be available to you. The next step is on every page where a user level check is required, at these lines to the top:
<?
session_start(); //you must start a session to use a session
if ($SESSION[username]) //if the user is logged in
{
$level=1;//you need to specify a level for this page
if ($SESSION[user_level] < "$level")// if this user's level below the level specified for this page
{echo "You are not authorized to view this page"; //Put anything here you want or redirect the user to another page using "header" function
}
else {
//here comes the page content
}
}
else { //if the user is not logged in}