Jump to content

Creating Dynamic Variables From MySQL


chrisrulez001
Go to solution Solved by Psycho,

Recommended Posts

Hi there,

 

I have a table in a MySQL database where I keep a list of user privileges. I am trying to create variables where the name of variable matches the privileges in the table.

 

This is also known as variable variables (I think).

 

EDIT (17/07/2014 04:02 PM): This might be a better way to describe what I'd like, so if the value from the table is admin_panel I'd like to dynamically create a variable with that name.

 

I have created a code so far, but all I seem to be getting is a list of Notice errors telling me that the variable is undefined. (I have supplied a list of errors a bit further down the post).

 

Here is the code:

<?php
$host = "localhost";
$account = "***";
$password = "****";
$dbname = "****";

$connect = mysql_connect($host,$account,$password) or die("Unable To Connect");
$db = mysql_select_db($dbname,$connect) or die("Unable To Select DB");

$perm_query = "SELECT * FROM `privileges`";
$permission_query = mysql_query($perm_query);

while($row = mysql_fetch_array($permission_query))
{
$rows[] = $row;
}

foreach($rows as $row)
{
${$row['privilege']};
}
?>

The list of errors:

 

Notice: Undefined variable: admin_panel in C:\xampp\htdocs\DynamicVariables.php on line 20


Notice: Undefined variable: create_user in C:\xampp\htdocs\DynamicVariables.php on line 20

Notice: Undefined variable: edit_user in C:\xampp\htdocs\DynamicVariables.php on line 20

Notice: Undefined variable: delete_user in C:\xampp\htdocs\DynamicVariables.php on line 20

Notice: Undefined variable: create_group in C:\xampp\htdocs\DynamicVariables.php on line 20

Notice: Undefined variable: edit_group in C:\xampp\htdocs\DynamicVariables.php on line 20

Notice: Undefined variable: delete_group in C:\xampp\htdocs\DynamicVariables.php on line 20

Notice: Undefined variable: view_log in C:\xampp\htdocs\DynamicVariables.php on line 20

Notice: Undefined variable: log_settings in C:\xampp\htdocs\DynamicVariables.php on line 20

Notice: Undefined variable: password_change in C:\xampp\htdocs\DynamicVariables.php on line 20

 

Thanks

Edited by chrisrulez001
Link to comment
Share on other sites

Because the following is trying to call the variable variable, which you have not defined it

${$row['privilege']};

If you are to do variable variables, you'd code it like so:

while($row = mysql_fetch_assoc($permission_query))
{
    ${$row['privilege']} = $row['privilege'];
}

What is the purpose of doing this? Not to be rude but it seems lazy to me

Edited by Ch0cu3r
Link to comment
Share on other sites

Ah right, thank you.

Well it sort of is a lazy way of doing things, its so that I can add, edit and delete privileges through an admin panel and because it reads the privileges from the database it automatically updates the variables and a HTML form, if that makes sense.

 

Thanks again

Link to comment
Share on other sites

Your code makes no sense. If I am reading it correctly the table has a single column being used called 'privilege'. I would assume then that each value is the name of a privilege? Heck, I don't even see anything in the query to only return the records for a specific user. But, you should have a finite list of privileges. I would have a table with a column for each privileged then a single record for each user with a 1 or zero in each column to identify if the user has that right or not.

 

You should think long and hard about the structure of what you are building. You could very well be taking yourself down a long dark alley where nothing good will come.

Link to comment
Share on other sites

Without trying to understand your code, I offer this suggestion.  Why not create a group of vars set to False - one for each of your privileges?  Then when you authenticate someone, turn on the vars (set them to True) that correspond to that user's privileges.

Link to comment
Share on other sites

Ok thank you for your time to post the suggestion.

I have been working on an idea. I have a list of privileges stored in the code, and I have a table where all the users information is and then have a privileges column and the privileges that the user can do is grouped together in that column and is called when the user is logged in.

Link to comment
Share on other sites

Yeah, that a pretty poor way to do that. I would still advocate a separate table to store permissions with a foreign key back to the user. Then have a separate column in the table for each permission. If you really want to have a single column to store the permissions, then you should use a bitwise operator. If you are familiar with binary, number are represented such as 10110 (that would be the number 22). Each digit in the number would represent a different permission. A 1 means the permission is granted and 0 is not

Link to comment
Share on other sites

Is there a security risk with doing it the way I have been?

 

I suppose if the table was injected you could see all the permissions but with doing it the way your saying, by referencing the ID of the permission in the users table rather than the actual permission.

So would this be a better solution?

Permission Table:

permissionID, permissionName

User Table:

username, password, permissions (random key), rest of columns...

Information table:

 

ID (random key for specific user which matches the random key in user table), permission_list (store list of permissions here in bitwise)

Then possibly do a foreach to grab the permission and whether the permission has been granted?

Link to comment
Share on other sites

  • Solution

There is no inherent security risk based upon how you store the data (there are always exceptions). It is the processes of how you store and read the data that add security risk. The problem with what you are doing is that it really limits how you can use the data.

 

I'm not really following your proposal today. If you are going to use a bitwise operator,then just store that in the user table and don't use the other two tables.

 

User Table:
username, password, permissions (bitwise values), rest of columns.

 

If you use a bitwise operator then you would need something to define which permission are at which position. You could either store this in PHP as part of a resource file or you could create a table in the DB. But, since you need to have logic in PHP to interpret the permissions anyway and this shoudl rarely, if ever change, I would do it in PHP.

 

The other approach is to simply have one associated table to the user table

 

User Table:

userid, username, password, rest of columns.

 

Permissions:

userid, createusers, createxxx, dosomething, etc.

Edited by Psycho
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.