Jump to content

Recommended Posts

Hello there all, I want to learn"How can i structure groups within a website? (groups like i mean-- admin ,moderators, helpers,users, etc-etc) ? " in context of PHP ... this site has the groups like this as i said, please someone provide me some concepts and ideas how i can implement this ?

Share this post


Link to post
Share on other sites

Obviously you have appropriate fields in your user database and then load session variables when the user logs in.

Share this post


Link to post
Share on other sites

You could have separate database tables for user types and permission lists, so that you can build some pages to create new user types and specify the permissions for each one, and then you can pick a user type for each user to give them those permissions.

Share this post


Link to post
Share on other sites

In your users table you should have a column called group or rang or whatever you want your admin to be named. After that you simply create a table called groups with your different ranks and functions, when you done that, you make sure that the id of the rang in the  groups table match the id the user has in it's column. For example you make 2 different groups, 

Super User, and Moderator. Super User has the id of 1 and can access all administrative pages, in that case you make an IF condition between the Super User record's ID in the table and the user's ID in the column. The script will be something like this:

if($_SESSION['user_rank'] == rank_required('page.php'))
{
	//display page
}
else
{
	//use a redirect method to a 404 not found or some kind of access restricted page
}

In the rank_required('page.php') will be a function that you will make and will check the rank's you've set for those pages, so that you make it a bit dynamically. But if you want an easy way, just set up it like:

if($_SESSION['user_rank'] == 1) //in our case the super user
{
	//display super user content
}
else
{
	//display error or redirect
}

/* Also you can do something like this: */

if($_SESSION['user_rank'] == 1)
{
	//display super user content
}
else if($_SESSION['user_rank'] == 2) //in our case the moderator rank
{
	//display moderator content
}
else //in our case none of the above
{
	//redirect page / error page
}

Easy enough, if you have any questions, feel free to ask, i personally didn't try the dynamically part as i never had a big website in which the dynamics would make a difference. But, in theory this should work pretty fine. I'm sure there are other methods out there.

Oh, and for the rank_required('page.php'); function, never made a function like that, but i assume in order to work properly you will need to have another table in your database called page_ranks, in which will be a column with the name of the physical name of the page, the rank required (id from the groups table). After that it's all about if conditions with database record for that page.

//Function

function rank_required($page) {
	try {
		$sql = "SELECT * FROM pages_ranks WHERE page_name = :page"; //first we crate our sql to the database, we assume you already have a db connection
		$stmt = $db->prepare($sql);
		$stmt->bindParam(":page", $page); //now we bind the paramter in the sql with the variable from our function that we get from a page through basename() function;
		$stmt->execute();
		$rowCount = $stmt->rowCount(); //warning as the rowCount(); function doesn't work if the previous statement that affected the db was an SELECT statement, but as long as you don't change records, i assume you will add all of your pages at one time, and then with the time the last sql statement would change, this should work just fine.
		if($rowCount > 0)
		{
			$row = $stmt->fetch(); //we fetch the result
			return $row['page_required_rank']; //this result will echo a value like: 1 or 2, depending on what id you have for your page in the page_required_rank collumn. Don't forget the ID depends on the group record's ID. And then this value will be compared into the header of the page with the $_SESSION['user_rank'];
		}
		else
		{
			return false;
		}
	} catch (PDOException $e) //catching possible errors
	{
		echo $e->getMessage();
	}
}

 

But if you don't want to get complicated with all of this database stuff you can just use the define function and you can change the ranks whenever you want through the variables you defined, this will make a lot easier to work with grades, still, you will need to edit the user's column in the database.

//Create a page like ranks.php and include it in everypage you need a rank system.

//ranks.php
define('SUPER_USER_RANK', '1');
define('MODERATOR', '2');

//page.php
if($_SESSION['user_rank'] == SUPER_USER_RANK) {
	//display super user settings
}
else
{
	//display error, redirect
}

//This helps you from later changing of the variable's value, so in that way you don't need to change the value from 1 to 2 or so on in everypage, you just have to change it in ranks.php to change the level of the required_rank.

This is just a concept, to help you understand how this thing works, of course this is some basic php scripting, as i said before, i'm sure there are more complex and way efficient ways to do this, but if you're a beginner, this might help. It helped me in the past, and still helps me now.

Hope i could help.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×