mediabob Posted June 25, 2007 Share Posted June 25, 2007 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 Quote Link to comment Share on other sites More sharing options...
chocopi Posted June 25, 2007 Share Posted June 25, 2007 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 ~ Chocopi Quote Link to comment Share on other sites More sharing options...
soycharliente Posted June 25, 2007 Share Posted June 25, 2007 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? Quote Link to comment Share on other sites More sharing options...
mediabob Posted June 25, 2007 Author Share Posted June 25, 2007 Thanks guys that is exactly what I was looking for, I was able to tweak it to work perfect for what I needed, I had the idea of what to do but couldn't implement it properly but your suggestions put me on the right track Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.