0

I have four different types of users login in to my website like admin, superadmin, company, and employees. Each of them have different set of pages to see but some common pages as well. Now I am having four different tables to manage them with same login screen for admin and superadmin. When either admin or superadmin logs in I will go and check two tables one by one before giving access. I have a separate login screens for company and employees. Is this the accepted way of doing it?

Actually, I want this to be changed to a single table with all users in it and a role table to differentiate the roles. I believe a four-table concept is really bad. I can't simply make it to one table because the previous developer had a habit of saving user comments and user activities in text files which is used on website.

Am I right in the way I think that a four-table login system is bad? Is storing logs in a text file that are directly used in website a good idea or not?

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Deepak
  • 6,450
  • 17
  • 65
  • 120

3 Answers3

5

You have 4 tables? Just use one user table and a field that can either be 'admin', 'superadmin', 'company' or 'employees'. Then you can have unlimited types of accounts. (I would do number codes like 1,2,3 or 4 instead of string codes or ENUM type field).

But yeah, your single table idea is fine. If you want a role table, do a foreign key to your role field and link it to your role table. You can have a single login too instead of different ones for different users and check for privileges based off that foreign key value.

mintobit
  • 2,353
  • 2
  • 13
  • 15
Kevin Wang
  • 3,220
  • 1
  • 26
  • 39
4

Here's my suggestion,

Instead of using four tables for your users, it would be better to use one.

You can create you basic user table like this (change rank to what suits your site/script):

ID username password email bla bla bla rank

So instead of using four tables, you can make your PHP script check if the user has the desired access level.

Here's a simple function to protect pages from lower access level users:

function required_level($level){
    $user_level = mysql_return(mysql_query("SELECT $rank FROM `Accounts` WHERE `user_id` =  $user_id"));
    if($user_level<$level){
        header("Location:index.php");
    }
}

Then on each page you want to protect from lower level users accessing it. You can just call required_level(4); and the page will only allow users with this level or over to access the page.

Example:

  • Bob is a employee so he has a user rank of 1,
  • Joe is a superadmin so he has a user rank of 4

Both users login normally, and both try to access admin.php.

admin.php starts with required_level(4); so Bob would be redirected to the home page (you can also pass an error) but, Joe would be bale to access this page because his rank is the same or above what is required to access this page.

So, here's my super long explanation on what you can do! I hope this helps and gives you some ideas on how to make your user tables better and easier to create protect pages :)

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
BExDeath
  • 104
  • 5
  • @Deepak What are you trying to log? log in's or what admin/users are doing? – BExDeath Aug 17 '12 at 02:51
  • if some user types in a comment it is now saved in a log file instead of database. Is this practice in any way accepted ? – Deepak Aug 17 '12 at 03:11
  • @Deepak If you want to have a comment system it would be best to hold all the comments in a mysql environment instead of having to create a whole system to read it off of saved files! – BExDeath Aug 17 '12 at 03:17
2

First of all, you can do the whole thing with a single table. In that table you should have fields like username, password, typeofuser and other necessary information.

Retrieve user information like:

$username = $_POST['username'];  //Retrieving a username from HTML login form
$row = mysql_query(sprintf("SELECT * FROM table WHERE username ='%s'", mysql_real_escape_string($username)));  //Retrieving a row from the database
$res = mysql_fetch_array($row);
$type = $row['typeofuser']; //Retrieving whether it is administrator, super administrator, user, etc.
if ($type == "admin")
    header(Loction:adminpge);

Similarly, you can check any type of user and can redirect to another page.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Anoop
  • 983
  • 5
  • 16
  • i have edited some part, if you have ny doubt feel free to ask. – Anoop Aug 17 '12 at 00:39
  • 2
    @Anoopss Golden If you post code, you should make sure it's correct / contemporary. You are using deprecated `mysql_*` functions and have a gaping sql injection hole. – jeroen Aug 17 '12 at 00:46
  • `mysql_query(SELECT * FROM table WHERE username ='$username');` and `header(Loction:adminpge);` are incorrect. forgot some quotes.. – Gabriel Santos Aug 17 '12 at 01:10
  • 1
    @Deepak - mysqli is technically deprecated too. The PHP dev team is set on PDO as the future, which is fine if you are targeting PHP 5.3 and later. But there are a ton of 5.2 and earlier hosts out there, so mysql_... functions are still relevant even if they are deprecated. You just have to be careful to not introduce SQL injection vulnerabilities, which newbie programmers tend to do - as demonstrated by Anoopss. In the hands of a professional, mysql_... functions are no worse or better than PDO. You can still do SQL injection in PDO and mysqli. – CubicleSoft Aug 17 '12 at 15:09
  • PDO as the future is really a great news!! PHP is moving from a casual environment to formal environment... – Deepak Aug 17 '12 at 22:05