Jump to content

* * - - - 2 votes

Defined Access Variables Tutorial

  • Please log in to reply
1 reply to this topic

#1 Jynxis


    Young Padawan

  • Members
  • Pip
  • 132 posts
  • Location:The Shadows

Posted 21 December 2006 - 04:03 AM

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.
$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.
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.
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...
$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; 

You could do something like this;
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

Edited by PlaGuEX, 21 December 2006 - 09:16 PM.

#2 Shaunyboy


    Young Padawan

  • Members
  • Pip
  • 6 posts

Posted 21 April 2007 - 04:02 PM

Hi, Thanks for this tutorial its coming in very handy :)

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users