Top-Rated Free Essay
Preview

Sample

Powerful Essays
1207 Words
Grammar
Grammar
Plagiarism
Plagiarism
Writing
Writing
Score
Score
Sample
A Better Login System - Tuts+ Code Tutorial

1 of 33

Tutorials

http://code.tutsplus.com/tutorials/a-better-login-system--net-3461

Courses

Premium

Jobs

Blog

Advertisement

Code

Categories

Software & Tools

Series

By Andrew Steenbuck, 26 Mar 2009
Tweet

0

Like

0

23

Net.tuts+ has published several great tutorials on user login systems.
Most tutorials only deal with authenticating the user, which allows for two levels of security: logged in and not logged in. For many sites, a finer degree of control is needed to control where users can go and what they can do. Creating an access control list (ACL) system will give you the flexibility for granular permissions.

Introduction

23/02/2014 21:26

A Better Login System - Tuts+ Code Tutorial

2 of 33

http://code.tutsplus.com/tutorials/a-better-login-system--net-3461

Imagine you are running a great tutorial site that lets users learn about a wide variety of web development techniques. In addition to your normal readers, you have some premium subscription members, as well contributing authors and administrators.

Your problem
You want to restrict users' to only specific pages that their particular account allows access to.

The solution
Implementing an access control list will allow you a great deal of control over what users can and cannot access on your site.
If you view the demo, available with the downloadable source code, you will be greeted with an index page that tests the ACL for each user. You can select different links at the bottom to view the ACL for the different users. If you click on the 'Admin Screen' link near the top, you can view a sample of the admin interface that allows you to manage the users, roles, and permissions. NOTE: The admin system will perform a database restore every 30 minutes to make sure everything stays on the up and up.
The download files also implement the ACL security on the admin site, so if user number one doesn't have the 'access admin' permission, you won't be able to access the admin site.
This system will enable you to create different groups of users (i.e. guests,

23/02/2014 21:26

A Better Login System - Tuts+ Code Tutorial

3 of 33

http://code.tutsplus.com/tutorials/a-better-login-system--net-3461

premium members, contributors, and admins). We will be able to set unique permissions for each group, as well as for individual users. Let's get started by setting up our MySQL database.

Step 1: Create the Database
Our ACL will be stored in a relational database using six tables (including the table for users). You should already have a database set up in your host environment. We will create the following table structure:

The code to create the database is available in the source files
(install.sql), and there is also another file (sampleData.sql) that will create
4 sample users, along with several roles and permissions for you to test with. Simply open the files with you favorite text editor, and copy/paste the code into the SQL panel in phpMyAdmin.

Step 2: Database Include
We need to create an include file so that we may connect to our database. Create a file called assets/php/database.php and add the following code to it (replace the variable values with the information appropriate for your hosting situation):
01
02
03
04
05
06
07
08
09
10
11
12
13
14

On the first line of the code, we call session_start(); we will not actually use the session variables but you will need it as part of the user login system. Then, we call ob_start() to create an output buffer. Typically, when PHP generates the page, it is sent to the browser as it is generating.
By using ob_start(), the page and headers aren't sent to the browser until they've loaded completely, or until we call ob_end_flush(). By buffering the page, we are able to redirect using PHP at any point on the page, instead of just at the top. After the headers are sent, our only redirect option is with JavaScript. An enterprising hacker could easily turn
JavaScript off, and then see our unsecured page in all it's glory. This one line allows us to deny the user access at any point in the page if needed.
Lines 4-8 set up our variables. $hasDB is a boolean used to determine if we are connected. $server, $user, $pass, and $db are the connection arguments for the server. Line 9 connects to the server, while line 10 determines if the connection was successful. If it was, we select the database to use; if it wasn't, we display an error message using die().

Step 3: Create the ACL Class
This step is fairly long, as we are creating the ACL class that will form the basis of our system. I apologize in advance for the length of this step.

23/02/2014 21:26

A Better Login System - Tuts+ Code Tutorial

5 of 33

http://code.tutsplus.com/tutorials/a-better-login-system--net-3461

Our ACL system will be object-oriented, so let's start creating the class file. We start by adding the class definition, variable definitions, and the constructor to the file /assets/php/class.acl.php:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22

above with some code to manage the users. We will use the querystring variable $action to determine which

23/02/2014 21:26

A Better Login System - Tuts+ Code Tutorial

12 of 33

http://code.tutsplus.com/tutorials/a-better-login-system--net-3461

of the user interfaces we should display. There are four possible values that we will address: If it is null, we display a list of the current users. If it is set to 'user', we display the form for a single user. If it is set to 'roles', we display the form to assign a user. If it is set to 'perms', we display the form to give the user permissions.

List Users
Add this code inside the div with the id 'page':
01
02
03
04
05
06
07
08
09
10

Select a User to Manage:

The concept here is pretty simple. We build a SQL query, run it and loop through the results. For each user, we generate a link that will enable us to edit that particular user.

Edit Individual User
Now, add this code directly under the previous code block:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

Managing :
... Some form to edit user info ...
Roles for user:
(

When we edit a user, we need to load the ACL for that user. This will enable us to see which roles and permissions they have. We start that by

23/02/2014 21:26

A Better Login System - Tuts+ Code Tutorial

13 of 33

http://code.tutsplus.com/tutorials/a-better-login-system--net-3461

creating a new ACL object, and passing in the $userID from the querystring (this way we load that user's ACL, instead of the logged in user). After that is where your normal edit user form would go. Typical things would be text fields to edit username, password, etc. Below that we list the roles the user is assigned to, and also provide a link so we can assign the user to other roles. Lines 10-16 load all the roles that the user is assigned to, and prints them out as list items using foreach(). Then we list out the user's permissions in a similar fashion. We only print out the permissions that the user has, not ones that are set to false.

Assign Roles
Our assign roles form will end up looking like this:

23/02/2014 21:26

A Better Login System - Tuts+ Code Tutorial

14 of 33

http://code.tutsplus.com/tutorials/a-better-login-system--net-3461

Add this code right below the previous code block:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

Manage User Roles: ()

MemberNot Member

You May Also Find These Documents Helpful

  • Powerful Essays

    Nt1330 Unit 5 Study Guide

    • 603 Words
    • 3 Pages

    Right click on Mikexxx user and make a print screen that shows his profile (indicating his assigned home directory).…

    • 603 Words
    • 3 Pages
    Powerful Essays
  • Better Essays

    Tipton, H. F., & Krause, M. (n.d, n.d n.d). Access Control Principles and Objectives. Retrieved November 29, 2013, from cccure.org: https://www.cccure.org/Documents/HISM/003-006.html…

    • 1902 Words
    • 8 Pages
    Better Essays
  • Powerful Essays

    SampleExam

    • 1710 Words
    • 14 Pages

    if you have a gif image it is better not to keep saving it as a gif…

    • 1710 Words
    • 14 Pages
    Powerful Essays
  • Good Essays

    lab 4

    • 503 Words
    • 2 Pages

    Of these three fundamental controls, which two are used by the Domain User Admin to create users and assign rights to resources?…

    • 503 Words
    • 2 Pages
    Good Essays
  • Good Essays

    • Multifactor Authentication – This method of authorization requires the user to provide more than one factor to log in. This is more secure than single factor authorizations, such as only requiring a password. For example, with multifactor authentication a user would need to provide another means of authentication in addition to a password.…

    • 838 Words
    • 4 Pages
    Good Essays
  • Powerful Essays

    Sample Question

    • 5066 Words
    • 21 Pages

    3. The Sarbanes-Oxley Act (SOX) of 2002 does not specifically prohibit an independent auditor from performing the following non-audit function(s) for an audit client:…

    • 5066 Words
    • 21 Pages
    Powerful Essays
  • Powerful Essays

    Sample Exam

    • 1778 Words
    • 8 Pages

    Jane Doe, who has substantial personal wealth and income, is considering the possibility of starting a new business in the chemical waste management field. She will be the sole owner, and she has enough funds to finance the…

    • 1778 Words
    • 8 Pages
    Powerful Essays
  • Powerful Essays

    The ACL is the Access Control List. Can be used to allow or deny access to objects by user or groups…

    • 906 Words
    • 4 Pages
    Powerful Essays
  • Satisfactory Essays

    Sample

    • 308 Words
    • 4 Pages

    TRENDS What’s happening in the world of iced beverages? We’ve looked at our data and we found out that... 1.…

    • 308 Words
    • 4 Pages
    Satisfactory Essays
  • Good Essays

    There are two main types of Access Control Lists; access ACLs and default ACLs. The access ACL are file or directory specific whereas the default ACL is associated only with directories. If an access ACL is not active on a file within a directory the file conforms to the rule assigned by the default ACL for that directory. Configuration of access control list can be can be…

    • 732 Words
    • 3 Pages
    Good Essays
  • Satisfactory Essays

    Statistical Sampling

    • 494 Words
    • 2 Pages

    4. Why do you think that it is not constitutional to use sampling techniques to count our current census? If we trust…

    • 494 Words
    • 2 Pages
    Satisfactory Essays
  • Satisfactory Essays

    Sample Quesstions

    • 219 Words
    • 2 Pages

    What process entails the altering of sensory information so that it can be placed in memory?…

    • 219 Words
    • 2 Pages
    Satisfactory Essays
  • Powerful Essays

    Sample Case

    • 2498 Words
    • 10 Pages

    Apply your critical-thinking ability to the knowledge you’ve gained. These cases will provide you an opportunity to develop your research, analysis, judgement and communication skills. You also will work with other students, integrate what you’ve learned, apply it in real world situations, and consider its global and ethical ramifications. This practice will broaden your knowledge and further develop your decision-making abilities.…

    • 2498 Words
    • 10 Pages
    Powerful Essays
  • Satisfactory Essays

    References: techNet. (2005, January 21). Access control in Active Directory. Retrieved from Microsoft TechNet: http://technet.microsoft.com/en-us/library/cc785913(v=ws.10).aspx…

    • 431 Words
    • 2 Pages
    Satisfactory Essays
  • Good Essays

    sample

    • 833 Words
    • 4 Pages

    (1) A few years ago, I watched a terrifying story on ABC's 20/20. It was a story about a teenage girl who had gotten pregnant. She successfully managed to keep her pregnancy from everyone, even her immediate family. She secretly gave birth to a baby girl -- by herself -- at the basement of her house. Out of fear, she covered the baby with a blanket and the baby died. There was an investigation and trial on the teenage girl, whether she should be convicted of murder or not.…

    • 833 Words
    • 4 Pages
    Good Essays

Related Topics