PHP
  Home arrow PHP arrow Creating a Secure PHP Login Script
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Creating a Secure PHP Login Script
By: Martin Tsachev
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 63
    2003-03-25

    Table of Contents:

    Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    In this article Martin explains how to create a secure PHP login script that will allow safe authentication. Features remember-me function using cookies, validates logins on each request to prevent session stealing.

    How Does This Work

    This is a short explanation why I have chosen these authentication methods.

    Users with shell access to the web server can scan valid session id's if the default /tmp directory is used to store the session data.

    The protection against this kind of attack is the IP check.

    Somebody who has a site (on a shared host with you) can generate valid session for your site.

    This is why the checkSession method is used and the session id is recorded in the database.

    Somebody may sniff network traffic and catch the cookie.

    The IP check should eliminate this problem too.

    Preparation

    You need first to decide what information to store about members, the examples provided will assume almost nothing to make it easier to read.

    I will use the PHP 4.1 super global arrays like $_SESSION, $_GET, etc. If you want to make it work on an earlier version of PHP you will have to substitute these with $GLOBALS['HTTP_SESSION_VARS'].

    Database Schema

    This is only an example bare structure suitable for online administration, if you want to have registered members you should add more columns.

    The schema is somewhat MySQL specific, I have yet to use another database other than MySQL and PostgreSQL but if you are using PostgreSQL you can convert the schema with the example script provided in my article Converting a database schema from MySQL to PostgreSQL.

    CREATE TABLE member (
      id int NOT NULL auto_increment,
      username varchar(20) NOT NULL default '',
      password char(32) binary NOT NULL default '',
      cookie char(32) binary NOT NULL default '',
      session char(32) binary NOT NULL default '',
      ip varchar(15) binary NOT NULL default '',
      PRIMARY KEY  (id),
      UNIQUE KEY username (username)
    );

    The password and cookie fields are md5 hashes which are always 32 octets long. Cookie is the cookie value that is sent to the user if he/she requests to be remembered, session and ip are respectively the session id and the current IP of the visitor.

    Connecting to the Database

    function &db_connect() {
     require_once 'DB.php';
     PEAR::setErrorHandling(PEAR_ERROR_DIE);
     $db_host = 'localhost';
     $db_user = 'shaggy';
     $db_pass = 'password';
     $db_name = 'shaggy';
     $dsn = "mysql://$db_user:$db_pass@unix+$db_host/$db_name";
     $db = DB::connect($dsn);
     $db->setFetchMode(DB_FETCHMODE_OBJECT);
     return $db;
    }

    This function connects to the database returning a pointer to a PEAR database object.

    Session Variables

    To ease access to the current user's information we register it as session variables but to prevent error messages and set some defaults we use the following function.

    function session_defaults() {
     $_SESSION['logged'] = false;
     $_SESSION['uid'] = 0;
     $_SESSION['username'] = '';
     $_SESSION['cookie'] = 0;
     $_SESSION['remember'] = false;
    }

    ... with a check like:

    if (!isset($_SESSION['uid']) ) {
     session_defaults();
    }

    to set the defaults. Of course session_start must be called before that.

    To the Core of the Script

    To allow easier integration with other scripts and make things more modular the core script is an object with very simple interface.

    class User {
     
    var $db = null; // PEAR::DB pointer
     var $failed = false; // failed login attempt
     var $date; // current date GMT
     var $id = 0; // the current user's id
     
    function User(&$db) {
      $this->db = $db;
      $this->date = $GLOBALS['date'];
      i
    f ($_SESSION['logged']) {
       $this->_checkSession();
      } elseif ( isset($_COOKIE['mtwebLogin']) ) {
       $this->_checkRemembered($_COOKIE['mtwebLogin']);
      }
     }

    This is the class definition and the constructor of the object. OK it's not perfectly modular but a date isn't much of a problem. It is invoked like:

    $date = gmdate("'Y-m-d'");
    $db = db_connect();
    $user = new User($db);

    Now to clear the code purpose, we check if the user is logged in. If he/she is then we check the session (remember it is a secure script), if not and a cookie named just for example mtwebLogin is checked - this is to let remembered visitors be recognized.

    Logging in Users

    To allow users to login you should build a web form, after validation of the form you can check if the user credentials are right with $user->_checkLogin('username', 'password', remember). Username and password should not be constants of course, remember is a boolean flag which if set will send a cookie to the visitor to allow later automatic logins.

     function _checkLogin($username, $password, $remember) {
      $username = $this->db->quote($username);
      $password = $this->db->quote(md5($password));
     
    $sql = "SELECT * FROM member WHERE " .
       "username = $username AND " .
       "password = $password";
      
    $result = $this->db->getRow($sql);
      
    if ( is_object($result) ) {
       $this->_setSession($result, $remember);
       return true;
      } else {
       $this->failed = true;
       $this->_logout();
       return false;
      }
     }

    The function definition should be placed inside the User class definition as all code that follows. The function uses PEAR::DB's quote method to ensure that data that will be passed to the database is safely escaped. I've used PHP's md5 function rather than MySQL's because other databases may not have that.

    The WHERE statement is optimized (the order of checks) because username is defined as UNIQUE.

    No checks for a DB_Error object are needed because of the default error mode set above. If there is a match in the database $result will be an object, so set our session variables and return true (successful login). Otherwise set the failed property to true (checked to decide whether to display a login failed page or not) and do a logout of the visitor.

    The logout method just executes session_defaults().

    Setting the Session

    function _setSession(&$values, $remember, $init = true) {
       $this->id = $values->id;
       $_SESSION['uid'] = $this->id;
       $_SESSION['username'] = htmlspecialchars($values->username);
       $_SESSION['cookie'] = $values->cookie;
       $_SESSION['logged'] = true;
       if ($remember) {
          $this->updateCookie($values->cookie, true);
       }
       if ($init) {
          $session = $this->db->quote(session_id());
          $ip = $this->db->quote($_SERVER['REMOTE_ADDR']);

          $sql = "UPDATE member SET session = $session, ip = $ip WHERE " .
             "id = $this->id";
          $this->db->query($sql);
       }
    }

    This method sets the session variables and if requested sends the cookie for a persistent login, there is also a parameter which determines if this is an initial login (via the login form/via cookies) or a subsequent session check.

    Persistent Logins

    If the visitor requested a cookie will be send to allow skipping the login procedure on each visit to the site. The following two methods are used to handle this situation.

    function updateCookie($cookie, $save) {
       $_SESSION['cookie'] = $cookie;
       if ($save) {
          $cookie = serialize(array($_SESSION['username'], $cookie) );
          set_cookie('mtwebLogin', $cookie, time() + 31104000, '/directory/');
       }
    }

    Checking Persistent Login Credentials

    If the user has chosen to let the script remember him/her then a cookie is saved, which is checked via the following method.

    function _checkRemembered($cookie) {
     list($username, $cookie) = @unserialize($cookie);
     if (!$username or !$cookie) return;
     
    $username = $this->db->quote($username);
     $cookie = $this->db->quote($cookie);
     $sql = "SELECT * FROM member WHERE " .
      "(username = $username) AND (cookie = $cookie)";
     $result = $this->db->getRow($sql);
     
    if (is_object($result) ) {
      $this->_setSession($result, true);
     }
    }

    This function should not trigger any error messages at all. To make things more secure a cookie value is saved in the cookie not the user password. This way one can request a password for areas which require even higher security.

    Ensuring Valid Session Data

    function _checkSession() {
     $username = $this->db->quote($_SESSION['username']);
     $cookie = $this->db->quote($_SESSION['cookie']);
     $session = $this->db->quote(session_id());
     $ip = $this->db->quote($_SERVER['REMOTE_ADDR']);
     
    $sql = "SELECT * FROM member WHERE " .
      "(username = $username) AND (cookie = $cookie) AND " .
      "(session = $session) AND (ip = $ip)";
     
    $result = $this->db->getRow($sql);
     
    if (is_object($result) ) {
      $this->_setSession($result, false, false);
     } else {
      $this->_logout();
     }
    }

    So this is the final part, we check if the cookie saved in the session is right, the session id and the IP address of the visitor. The call to setSession is with a parameter to let it know that this is not the first login to the system and thus not update the IP and session id which would be useless anyway.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More PHP Articles
    More By Martin Tsachev

     

    IBM® developerWorks developerWorks - FREE Tools!


    Check out the new Jazz space on developerWorks

    <a href="http://zeus.developershed.com/shonuff.php?blackbird=3853&zoneid=442&source=&dest=http%3A%2F%2Fwww.ibm.com%2Fdeveloperworks%2Fspaces%2Fjazz%3FS_TACT%3D105AGY31%26S_CMP%3DDEVSHED&ismap="><img src="http://images.devshed.com/corp/img/news/jazz01.gif" alt="developerWorks Jazz space" align="left"></a>You've heard the buzz about Jazz... want to know more about it from a developer's perspective? Check out the Jazz space on developerWorks. This space is an up-to-date resource for developers, including technical information about Jazz and products built on Jazz, like Rational Team Concert Express. The Jazz space includes content from a wide variety of sources, including links, feeds, and comments from experts.
    FREE! Go There Now!


    NEW! IBM – Taking Web 2.0 to Work

    David Barnes, Lead Evangelist for IBM Emerging Internet Technologies will discuss aspects of Web 2.0 that bring value to corporations, academia, and government. He'll also discuss IBM's vision around Web 2.0, including the importance of remixability and consumability. The discussion will culminate with examples of various IBM Software Group solutions you can use to get ahead of the Web 2.0 adoption curve.
    FREE! Go There Now!


    NEW! Download the free Web Application Security eKit

    Discover how IBM Rational AppScan Standard Edition can help you detext vulnerabilities in your web applications in the Web Application Security eKit. IBM Rational AppScan is a leading suite of automated web application security solutions that scan and test for common Web application vulnerabilities. The new Web Application Security eKit provides you with valuable resources, including white papers, demos, and additional information on the benefits of testing your Web applications.
    FREE! Go There Now!


    NEW! Harnessing the power of SQL and Java for high performance data access

    Join this webcast to see how IBM Data Studio Developer and pureQuery can take the pain out of Java data access. uApplications developed using both Java and SQL have become a common requirement. Database connectivity using Java Database Connectivity (JDBC) to create an application is a multi-step tedious process, and tooling that covers both SQL and Java has been unavailable, until now. IBM Data Studio introduces the pureQuery platform: a high-performance, Java data access platform focused on simplifying the tasks of developing, managing, and optimizing database applications and services.
    FREE! Go There Now!


    NEW! Rational 'Talks to You' Teleconference Series

    This Fall, IBM Rational talks to you directly through a special teleconference series giving you access to the best minds in IBM Rational - product experts and market thought leaders who will answer your questions during these pre-scheduled telephone conference calls. Register today!
    FREE! Go There Now!


    NEW! Software Change and Configuration Management Solution Guidelines

    This whitepaper provides areas to consider when evaluating any software configuration management solution. It addresses how the IBM solutions (Rational ClearCase and Rational ClearQuest) meet the needs and requirements of both project leaders and developers to provide successful Software Change and Configuration Management.
    FREE! Go There Now!


    NEW! Trial download: IBM Informix Dynamic Server Express Edition V11.0

    Informix Dynamic Server (IDS) Express Edition offers outstanding online transaction processing (OLTP) database performance, while helping to simplify and automate many of the tasks associated with deploying databases for small business applications. IDS 11 further extends the ease of management and applications integration with the Admin API and Scheduler, high availability with Continuous Log Restore for backup server recovery in case of a primary server failure, and column level encryption to protect personal and company private data.
    FREE! Go There Now!


    NEW! Trial download: IBM Rational Tester for SOA Quality V7.0.1

    Get a free trial download of the latest version of IBM Rational Tester for SOA Quality V7.0.1, a functional and regression testing tool that enables the creation, comprehension, modification and execution of testing GUI-less Web services.
    FREE! Go There Now!


    NEW! Webcast: Extreme transaction processing with WebSphere Extended Deployment

    In this webcast, you'll get an introduction to the eXtreme Transaction Processing (XTP) features of WebSphere Extended Deployment and the common architectural traits required by XTP applications. See how WebSphere Extended Deployment's ObjectGrid feature provides a state-of-the-art infrastructure for hosting XTP applications.
    FREE! Go There Now!


    NEW! Webcast: What is new in Viper 2 for developers?

    Viper 2 brings a great value to developer communities including SQL, XML, PHP, Ruby, .NET and Java. You probably already know that DB2 Express-C is free for developers to develop, deploy and distribute. Viper 2 provides a variety of means that help move your application from the development stage to deployment more rapidly. This webcast shows how to best utilize the latest tools available for developing DB2 applications.
    FREE! Go There Now!



    All FREE IBM® developerWorks Tools!

    PHP ARTICLES

    - Making Usage Statistics in PHP
    - Installing PHP under Windows: Further Config...
    - File Version Management in PHP
    - Statistical View of Data in a Clustered Bar ...
    - Creating a Multi-File Upload Script in PHP
    - Executing Microsoft SQL Server Stored Proced...
    - Code 10x More Efficiently Using Data Access ...
    - A Few Tips for Speeding Up PHP Code
    - The Modular Web Page
    - Quick E-Commerce with PHP and PayPal
    - Regression Testing With JMeter
    - Building an Iterator with PHP
    - PHP Frontend to ImageMagick
    - Using PEAR's mimeDecode Module
    - Incoming Mail and PHP







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT