Jul 20, 2008

User Permission Function [php] - Determining User Permissions

Free Web Hosting, No Ads > CONTRIBUTE > Tutorials

free web hosting

User Permission Function [php] - Determining User Permissions

jlhaslip
There have been several recent request for methods to restrict access to various pages on a web-site based on User Permissions in sites that have a Login System in place. Here is some code showing one method to restrict access by providing a sub-set of your site's links based on the User's permissions.

In this demonstration, I have defined a string for each 'level of user' determining which set(s) of links they can view on your site.
Normal MySql procedure would be to read the User's permission level from a record in the database.
For the purpose of this demonstration, an array is constructed. The output reflects the various combinations of their permissions.
The strings are a series of concatenated Boolean values for the permissions allowed by a group. 0 = false (no links) 1 = true (links).
As you can see, the Guests get 'some' links, Members get 'some more' links, Mods get 'still more', and the Admins get 'all' links.

Array values can be read from the User Profile or from $_SESSION values, or from a 'group' table depending on their group membership.
Each member is assigned to a Group and their permissions are given according to the Group they belong to.
Whether the permissions string is stored with the member Profile ( not suggested ) or in a separate Table in the database ( suggested ) does not really affect the function of this procedure, as long as the permissions function is fed the correct of string.

Replace these simple echo commands with the code which will generate the links for your site.
And normally, you would not use the else statements, so those could be removed from the code, too.
Adjust the details and particular to suit your own needs.
Additional Permissions could be set for allowing certain Groups the right to add, edit, delete stuff, or Ban Users, etc. I'll leave that to you, since it would be very site specific.

Happy Coding. smile.gif

CODE

$levels = array(
'Guests' => "1000" ,
'Users' => "1100",
'Mods' => "1110" ,
'Admins' => "1111" ,
);


/*
*Function to assign Permissions based on Group membership
*/

function permission( $p )
{
// Split the levels string into an array
$perm = str_split( $p );

// Permissions array
$up = array(
'can_view_links_1' => 1,
'can_view_links_2' => 2,
'can_view_links_3' => 3,
'can_view_links_4' => 4,
);
$combine = array_combine( array_keys( $up ), $perm );

return $combine;
}

/*
* Procedure Test Printing
*/

foreach ( $levels as $i => $value) {

/* print Group name here */
echo '<h3>' . $i . '</h3>' ;

/* call function to assign permissions */
$p = permission( $levels[$i] );

/* check Guest permissions */
if( $p['can_view_links_1'] == 1 ){
echo 'can view Guest links&nbsp;&nbsp;' . $p['can_view_links_1'] . '<br />';
} else {
echo 'no links here&nbsp;&nbsp;' . $p['can_view_links_1'] . '<br />';
}

/* check Member permissions */
if( $p['can_view_links_2'] == 1 ){
echo 'can view Member links&nbsp;&nbsp;' . $p['can_view_links_2'] . '<br />';
} else {
echo 'no links here&nbsp;&nbsp;' . $p['can_view_links_2'] . '<br />';
}
/* check Moderator permissions */
if( $p['can_view_links_3'] == 1 ){
echo 'can view Moderator links&nbsp;&nbsp;' . $p['can_view_links_3'] . '<br />';
} else {
echo 'no links here&nbsp;&nbsp;' . $p['can_view_links_3'] . '<br />';
}

/* check Administrator permissions */
if( $p['can_view_links_4'] == 1 ){
echo 'can view Admin links&nbsp;&nbsp;' . $p['can_view_links_4'] . '<br />';
} else {
echo 'no links here&nbsp;&nbsp;' . $p['can_view_links_4'] . '<br />';
}
}

?>


Here is a demo using the above script.
http://www.jlhaslip.trap17.com/permissions/

Any questions, post them up here.

 

 

 


Reply

hippiman
A couple of friends and I just did a programming competition using PHP and we just used a cookie with 3 values, for the different kinds of users.

We didn't make it so that it didn't show the links, though. We just made each page show a thing saying that they don't have permission to use that page. I think what you're doing makes sense, too, though. I think you should also have each page deny access for if they just type in the right url, though.

Nice tutorial.

Reply

jlhaslip
Thanks.

The site checks Sessions and Permissions both to deny access. I'll post the full version later as I complete the package.

Might take a few days/weeks since it is a hobby, not a job.

Reply

alex1985
For instance, if I have a simple login system with control panel. Can I merge this permission script with my login system or not? Write the tutorials as well as if you can.

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Recent Queries:-
  1. user permission in php - 35.19 hr back. (1)
  2. php user permission - 42.31 hr back. (1)
Similar Topics

Keywords : user, permission, function, php, determining, user, permissions

  1. Php - Forms, Date And Include
    Working with POST and GET and also the Date() function (0)
  2. Php - Randomize Web Title
    using arrays and the random function (5)
    PHP - Randomize Web Title with the rand function... Define the variables numbers we start at number
    0 CODE <?php $title[0] = "Web title 0"; // Title 0
    $title[1] = "Web title 1"; // Title 1 $title[2] = "Web title
    2"; // Title 2 $title[3] = "Web title 3"; // Title 3
    $title[4] = "Web title 4"; // Title 4 ?> now we use the rand()
    function to get a randomize the web title. rand(0, 4) means we start at 0 and end at 4 i can get
    0,1,2,3,4 one of those. ....
  3. Simple User System
    php, mysql driven (19)
    Hey! Maybe you've seen my other tutorials...or my signature.. Anyways I'm going to show
    you how to make a system so users of your site could register accounts and you could have protected
    - user only - pages on your site /smile.gif" style="vertical-align:middle" emoid=":)" border="0"
    alt="smile.gif" /> Ok, so we start by creating a config.php file. CODE <?php
        $dbhost   = 'database host';     $dbname   = 'database name';
        $dbusername   = 'database username';     $dbuserpass = 'database pas....
  4. *nix File Permissions - An Overview
    (6)
    I was originally going to post this in a reply, but felt it would deviate from the topic.
    Here's a brief overview of the three numbers in a permission "code": -The first number is for
    the owner of the file. If you set a file at 600, the owner will have read and write access and
    everyone else is locked out. -The second number is for the users group (users are placed into groups
    to get special rights sometimes). Generally you will not give write access to a user's group.
    -The third number is for the rest of the world, including web users. Setting any value that wi....
  5. Flatfile User Login/signup
    Uses text files only (compatable with forums and message system) (24)
    With this tutorial, you will learn how to create a textfile login script. This user membership
    script is for use also with my forums and message system scripts. I will also give you the scripts
    to make it so that people can change their profiles. Ok, The first thing we need to do is make the
    database. To do this, create a blank text file called 'userdata.txt' , make sure it is ALL
    LOWER-CASE. Edit this file and put
    '**username|##|password|##|email|##|rank|##|userid|##|name|##|picture**'. This will not be
    used, however it will give you an idea of how the....
  6. How To "lock Down" A Os X User Account
    Crude but effective way to maintain Macs (1)
    Here's a quick summary of how one can configure OS X for use in public labs running Panther
    (10.3). It should also work with Tiger (10.4) but I dunno. There may be better ways, but this is
    quick and cheap: 1. Install OS X fresh, or boot up your new Mac, and set the username to
    MacAdmin or the like. This is now the administrator account which users should never touch.
    Share this password only with trusted admins authorized to muck with critical systems. 2.
    Install all the software you expect anyone to need in the default folders (usually Applicat....
  7. Hiding User Account On Xp
    (0)
    Hopefully someone will find this useful. Ive done this on my machine as well. To a hide an account
    to display on the welcome screen of xp here is a nifty way: 1. Open regedit 2. Browse to
    Hkey_local_machine> software>microsoft>windows nt>winlogon>special Accounts>Userlist Here you will
    see a list of all accounts on XP which are hidden and dont display on welcome screen. Yes XP does
    create useless accounts as you see there. 3. Go on the right pane, right click, create a new dword
    value with its name as the exact username you want to hide and its value as "0" without t....
  8. How To: Change An Image When A User Clicks On It
    using both php and javascript (11)
    How To: Change An Image When A User Clicks On It using both php and javascript - a powerful
    combination I have seen quite a few how tos offering a method of doing this but none of which
    resembled my method of making use of both php and javascript. This code is fairly repetitive and
    most of the functions are easy to pick-up if you haven't heard of them before. Here it is...
    Create your two images. Call them anything you like, you'd just need to change their filenames
    in $imgano $imgayes. In fact with this script you can easily create more tha....
  9. Simple Login In Visual Basic 6
    user interaction example trough login programm (5)
    First of all, I am NOT a programmer, this is something my friend taught me. It describes basic
    interaction with the user, while showing basic functionality of this simple programm. So, without
    further ado, we're off to the tutorial: First of all, start your visual basic, when prompted
    for new project, select Standard Exe . Next, we need to open code window, so we can start typing
    the program. This can be done in two ways, one is double clicking on the form, or selecting Code
    from View menu. If you double clicked on the form, you will see following text: CODE ....
  10. Php Sockets
    Introduction to an underused function of PHP (4)
    This tutorial is based on a question written in the PHP programming board Inspiration
    Introduction Sockets, an underused function in PHP, is a function that enables you to open
    connection to other peoples computers and vice versa. By sending commands to the operating server,
    it will first see if there's response, then, it sends or receives data that the operating server
    has available. Creation of a socket Of course, a first step in using sockets, is creating it,
    this is done by: CODE resource socket_create ( int domain, int type, int protocol) ....
  11. Using The Date() Function [php]
    (2)
    The date() function can be used in a good way, in some cases, and the most common place you would
    probably see this in use would be on forums. Let's get down to business... If you would like to
    display the date, you would have to put the echo tag before the date() function as so... CODE
    echo date("g:i:s"); Which would display as the common hours, minutes,
    seconds format (ex: 3:32:45). But you can also add wheather it is AM or PM, display the day (Sunday
    thru Monday), the year, pretty much anything that has to do with date. Here is a....
  12. [tutorial] Delphi
    Multi-Parsing Function (0)
    I've spent a while trying to create parsing function with Delphi. This language is very useful.
    This tutorial explains how to parse multiple instances of an item in one string. Here is the
    function (which I am actually very proud of): Code: function TForm1.MultiParse(MainString,
    BeginString, EndString: string): TStrings; var PosBeginString, PosEndString, i, LastPos: integer;
    tmpCopy: string; tmpStrings: TStrings; begin tmpStrings := TStringList.Create; LastPos := 0;
    for i := 1 to LastDelimiter(EndString, MainString) do begin PosBeginString := Pos....
  13. Delphi
    Simple Text Parsing Function (1)
    Because parsing is such an integral part of string manipulation, I took the time to make a quick and
    simple parse function. For you VBers, Delphi is a derivative of Pascal...it's powerful, simple,
    and (best of all) doesn't need external component (eg: ocx files). Hence the code: Code:
    function TForm1.SimpleParse(MainString, BeginString, EndString: string): string; var PosBeginString:
    integer; PosEndString: integer; begin PosBeginString := Pos(BeginString, MainString) +
    Length(BeginString); PosEndString := Pos(EndString, MainString); Result := Copy(MainString, ....

    1. Looking for user, permission, function, php, determining, user, permissions

Searching Video's for user, permission, function, php, determining, user, permissions
advertisement



User Permission Function [php] - Determining User Permissions



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE