Jump to content

[SOLVED] Help with member levels


mediabob

Recommended Posts

Hi, I am just learning to work with sessions for a user login script and there is something I need help with, I want to be able to have content on some pages that is only visible if you are either logged in, or logged in for a certain member level, for example:

 

I want guests to be able to see the news on the front page but not the comment and edit links

I would like members to be able to see the news and the comment link, but not the edit link

I Want Admins to be able to see all 3 things.

 

Sort of like most CMS sites are.

 

I have tried downloading some example scripts from various script repository sites to try and learn how something like this might be done, but the only scripts I can find protect a whole page, so it's see it all or see nothing unless you are logged in.

 

I have an idea, but not sure how to implement it (or if it is even a good one)

 

Would it be possible to create a session as soon as someone visits the site and maybe set a variable like

username = guest

memberlevel = 0

and then update the variable to the correct level once they login and then use conditionals on the sensitive links like show this is memberlevel=this?

 

If someone cab help, thanks in advance :)

Link to comment
Share on other sites

what i would do is store all the ranks in a table and then in your user table store their rank ids (or something similar). Then on the page check if the user is logged in, if not then they are level 1, if they are level 2 and if they are admins then level 3.

 

Then just use if statements

 

<?php

$username = 'Chocopi';
$query = mysql_query("SELECT field FROM table WHERE username='$username'") or die(mysql_error());
$row = mysql_fetch_assoc($query) or die(mysql_error());
$user_level = $row['field'];

if($user_level >= 1)
{
   // show news
} else
   if($user_level >= 2)
   {
       // show comment link
   } else
      if($user_level == 3)
      {
          // show edit link
      }

?>

 

Something like that should work ;D

 

~ Chocopi

Link to comment
Share on other sites

Would using a case statement be faster/more efficient?

<?php
switch ($user_level) {
case 0:
	showNews();
	break;
case 1:
	showComments();
	break;
case 2:
	showEdits();
	break;
default:
	hackUserInformationAndSendOutThousandsOfSpamEmails();
	break;
}
?>

That last part is a joke ;)

Why is the color coding not working?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.