Recent Articles

Get Google Reader feeds with PHP

 

Simple PHP access control

Access control can be a complicated process.  Taking a “simple, but not simpler” approach allows extensibility in this basic task of web application development.

Read on
 

EE’s {weblog_short_name} and CSS

Sometimes CSS can be used in very creative ways, especially when there’s dynamic content.

Read on
 

DOM Trick: de-thumb an image URL

When ExpressionEngine auto-creates a thumbnail image, it gives the option for creating the markup to display the full size image in a new, pop-up window or in a blank window.  If you’re like me, those options aren’t optimal.  So I put together a handy 4-line Javascript DOM trick to give me other choices.

Read on
 

Resource: Findable Websites

There’s many books and resources available on designing client- and server-side web sites, but few which focus on the practical methods of creating web sites that users can find.  This is one worth mentioning.

Read on
 

Simple PHP access control

Access control can be a complicated process.  Taking a “simple, but not simpler” approach allows extensibility in this basic task of web application development.

This approach for controlling access to a web application is a simple two-part loop:

  • user not logged in: display login form
  • user is logged in: display application

There’s nothing new here; I’m simply documenting an approach I have found easy to implement.  In it, the user loads the application’s index.  The index’s code asks, “Is the user logged in?”  If the answer is “yes” the application’s main function is called.  If “no” the function to display the login form is called.  Using this method, the user never gains access to any of the application’s functionality until they pass the login test.  Security is built into the method by design.

For the purposed of the demonstration, we will use three (3) files:

  • index.php
  • login.php
  • functions.php

The “functions.php” file is where the functions for displaying the login form and for starting the application are located.  The “login.php” is used to grant access to the application or to log out a user.  It’s a separate file and, again, security is built into the method.

Here’s a diagram which shows the path the user takes through the method:

image

Now, let’s look at the code involved:

File: index.php

<?php
    session_start
();                // if necessary

    
include_once "functions.php";
 
    if (
user_is_loggedin()) {
        do_application
();            // app's main function
    
}else {
        display_login_form
();
    
}
?> 

Here, we simply check the value returned from the user_is_loggedin() function to determine what to do.

File: login.php

<?php
    
include_once "functions.php";
    
    if (isset(
$_POST['username'])) {
        setcookie
("username"$_POST['username']);
    
}else{
        setcookie
("username""");
    
}
    header
("Location: index.php");
?> 

The “login.php” file is called from the “display_login_form()” function in the “functions.php” file (code below).  The code here either creates or destroys a cookie.  The cookie is used in the “user_is_loggedin()” function, also below.

File: functions.php

<?php
    
function user_is_loggedin() {
        
if (isset($_COOKIE['username'])){
            
return true;
        
}else{
            
return false;
        
}
    } 
// end user_is_loggedin()
    
    /* Displays login form
     * *******************
     */
    
function display_login_form() {
        $html_str 
"<div id=\"login_form\">"
        
"<h2>Login to Application</h2>";
        . 
"<form action=\"login.php\" method=\"post\">\n";
        . 
"<p>Username: <input type=\"text\
            name=\"
username\" value=\"\" /></p>\n";
        . 
"<p>Password: <input type=\"password\
            name=\"
password\" /></p>\n";
        . 
"<p><input type=\"submit\" value=\"Login\" /></p>\n";
        . 
"</form>\n";
        . 
"</div>\n";

        echo 
$html_str;
    
// end display_login_form()
    
    /* Main function
     * *************
     */
    
function do_application() {
    
/*
     * Functionality of application
     */
     // Logout button
        
$html_str .= "<form id=\"logout\" action=\"login.php\">\n"
        
"<p><input type=\"submit\" value=\"Quit\" /></p>\n"
        
"</form>\n";
        echo 
$html_str;

    
// end do_application()
?> 

In “user_is_loggedin()”, we check to see if a cookie has been created for the user, meaning the user has logged in.  You’ll notice also the “logout button” code in the main application function.  As is does not contain a form field named “username,” the code in “login.php” evaluates to false.  This calls code to destroy the cookie created during the login process.

To sum things up:

  • We check to see whether a user is logged in.  If “yes”, we start the application.  If “no” we ask them to log in.
  • If user is logged in and wants to log out, we destroy the user cookie, forcing the display of the login form again.

This access control method is simple and can be easily expanded to provide storage of user settings in database and the creation/management of user groups.  It is based upon the question, “Is the user logged in?” and the use of a client-side cookie.

11/10 at 08:35 AM

Comments

  No one's commented yet. Add yours!

Add Comment

Commenting is not available in this channel entry.
© 2002-2010 | carvingCode ™ | carving unique nooks in the web ™ | simple, but not simpler ™