Jump to content

[SOLVED] a simple user access level


richiec

Recommended Posts

Hey people, what i am trying to do is just make a very simple user access level. Ill explain as much as i can for you to understand.

 

In mysql db i have a simple table for users, with 4 fields;

 

1) id

2) username

3) password

4) access

 

My login system works fine that isnt a problem, now in the access field what i am trying to do is in there either have "admin" or "member" depending on the user.

 

The reason behind this is so in the php members area it echos different things depending on that users access (admin or member in the access field)

 

something like this...

 

<?
$nick = $_SESSION["myusername2"];
$query="SELECT * FROM `user` WHERE 'username' = $nick";
$result=mysql_query($query) or die(hmm);
$num = mysql_num_rows($result);

if (mysql_result($result,$i,"access") == 'admin'){

echo "<admin user page>"; }

elseif (mysql_result($result,$i,"access") == 'members'){

echo "<members user page>"; } 
?>

 

any ideas?

 

 

Thanks

Link to comment
Share on other sites

<?php
$nick = $_SESSION["myusername2"];
$query="SELECT access FROM `user` WHERE 'username' = $nick";
$result=mysql_query($query) or die(hmm);
$num = mysql_num_rows($result);


$isAdmin = ($result['access'] == "admin")?true:false;

if ($isAdmin) {
echo "<admin user page>"; }
else{
echo "<members user page>"; } 
?>

 

A note <? has been depreciated, use <?php

 

That is a basic usage given there are only 2 roles, admin and member. Then anytime you need to display admin feature you check isAdmin.

Link to comment
Share on other sites

you could use switch

 

<?php
$nick = $_SESSION["myusername2"];
$query="SELECT access FROM `user` WHERE 'username' = $nick";
$result=mysql_query($query) or die(hmm);
$num = mysql_num_rows($result);

switch($result['access'])
{
   case admin:
      // admin page
      break;
   case mod:
      // mod page
      break;
   case normal:
      // normal page
      break;
}
?>

 

if you get rid of the break's then the admin would see admin,mod,normal and mod would see mod,normal and normal would just see normal. If that makes sense :P

 

replace admin, mod and normal with whatever values you are expecting.

 

Hope that helps ;D

 

~ Chocopi

Link to comment
Share on other sites

A note <? has been depreciated, use <?php

 

what!?!?!?!?

 

 

I guess to say they have been depreciated would be incorrect, there has been talk on php.net stating that it may be in the future so code for it:

 

Short open tags, register globals, and magic quotes (perhaps others) were lazy-way short cuts to get the language to do things that the programmer should have been doing himself (typing a few characters, referencing the proper variable array, and escaping special characters in only the cases where they needed to be escaped.) All three of these have resulted in more long term work to troubleshoot and fix then what they saved in time in the short term. Aside from the disadvantages introduced by these three things, they also unfortunately introduced multiple ways of accomplishing the same thing that are now dependent on server settings to work.

 

Basically <?php  is the proper cross-server way to code as it will work on any php server, the short tags may not work due to server settings etc.

Link to comment
Share on other sites

Now i just get a blank page but atleast it gives me no errors which probably just means its a html layout error or something so i should be able to figure it out. Thanks alot for all your help! Enjoy the rest of your day/night

 

Rich.

 

Remove the < and > from the output or view the source.

 

Remember that < and > are considered html tags and will be hidden in the source of a page and not displayed.

Link to comment
Share on other sites

Sorry to reopen this, but i still cant get it, ive changed it to the if/else command insted of the switch command but still cant get it.

 

I have just set it to do something simple once i get it to work i can put it all back into place with the right layouts for admin/members ect this is what i have..

 

$nick = $_SESSION["myusername2"];
$query="SELECT `access` FROM `user` WHERE `username` = '$nick'";
$result=mysql_query($query) or die(mysql_error());
$num = mysql_num_rows($result);

$isAdmin = ($result['access'] == "admin");

if ($isAdmin) 

echo "admin test"; 

else 

echo "members test";

 

Now i have loged in as someone with admin and someone with member as there access but on both it echos "members test" it doesnt echo "admin test" on the admin user. Any ideas?

Link to comment
Share on other sites

Your missing the point of an earlier post by premiso.

 

$isAdmin = ($result['access'] == "admin")?true:false;

 

That uses what is called a ternary operator. the ? and : which is basically a shortened if/else.

 

I suggest you change:

 

$isAdmin = ($result['access'] == "admin");

TO

$isAdmin = ($result['access'] == "admin")?true:false;

 

and give it a try.

Link to comment
Share on other sites

$nick = $_SESSION["myusername2"];
$query="SELECT `access` FROM `user` WHERE `username` = '$nick'";
$result=mysql_query($query)) or die(mysql_error();
$num = mysql_num_rows($result);
$result = mysql_fetch_array($result);

$isAdmin = ($result['access'] == "admin");

if ($isAdmin) 

echo "admin test"; 

else 

echo "members test";

 

Forgot to fetch the array of the query result, try that.

Link to comment
Share on other sites

<?php
$nick = $_SESSION["myusername2"];
echo "Username is: " . $nick;
$query="SELECT `access` FROM `user` WHERE `username` = '$nick'";
$result=mysql_query($query)) or die(mysql_error()); // error here fixed
$num = mysql_num_rows($result);
$result = mysql_fetch_array($result);

print_r($result);

$isAdmin = ($result['access'] == "admin");

if ($isAdmin) 

echo "admin test"; 

else 

echo "members test";
?>

 

Try some debugging procedures, make sure what is expected to check against is being checked and that you are getting a result. Also check the case on admin as php is CaSeSenSitIvE.

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.