Help - Search - Members - Calendar
Full Version: Defined Access Variables Tutorial
Pixel2Life Forum > Member Tutorials and Requests > Forum Tutorial Archives > PHP Tutorials
Jynxis
Sorry if i dont explain this tutorial too well, i usually just write the code, and have a friend(who knows php) write if for me.lol.
Now onto the tutorial, if you could call it that.


This is useful for when you have a user access restriction to certain pages, and you dont want to use numbers, which at some point, might get a lil confusing. You may start to wonder if, 1 = admin or 5 = guest. Or the other way around.
You might start to clutter up with something like this.For Example.
CODE
$USER_LOGGED_IN means nothing, in this tutorial.But it can be interpreted as a replacement for the user's name/id that is logged in at the moment from a COOKIE/SESSION that is initialized by the user.

CODE
if(UserAuthentication($USER_LOGGED_IN, 0)){ //  Zero (0) Being the access needed
    echo("Welcome Member");
}


With this example, you can define the variable so that it can be identified easily.For Example it could be done like this.
CODE
if(UserAuthentication($USER_LOGGED_IN, $user_access['member'])){ //  $user_access["name"] Being the access needed
    echo("Welcome Member");
}

But that still wouldnt look nice.

But with this code below...
CODE
<?php
$USER_ACCESS = array(); // Create User Access Array
// Create Access List Array
$access_levels = array("0" => "Guest",
                       "1" => "Member",
                       "2" => "Special Member",
                       "3" => "Global Moderator",
                       "4" => "Administrator");
// Insert Access List into the User Access Array
// Create User Access Array
$USER_ACCESS = array_flip($access_levels);

/*This is Another Way to Define The Access Array
foreach($access_levels as $key => $value){
        $USER_ACCESS[$value] = $key;  
    }
//*/
// Start Defining the access. To create  a new variable. EG: SPECIAL_MEMBER
    foreach($USER_ACCESS as $key => $value){
        $DEFINE_KEY = str_replace(" ","_", strtoupper($key));
        $DEFINE_VALUE = $value;
        define($DEFINE_KEY,$DEFINE_VALUE);
    }
?>


You could do something like this;
CODE
if(UserAuthentication($USER_LOGGED_IN, GUEST)){
           echo("This page can be publicly viewed.");
}

It wouldnt take much modifying to turn the hard-coded "$access_levels" array list into something taken from a database.
Sorry if its not really sounding like a tutorial, but...
1) Im tired
2) I dont usually write the tutorials, but the code for the tutorials.

Gimme pointers on makin this sound more tutorialish.

Hope this helps someone
Shaunyboy
Hi, Thanks for this tutorial its coming in very handy victory.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.