JavaScript
  Home arrow JavaScript arrow Page 3 - Building a CHAP Login System: Coding Serve...
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? 
JAVASCRIPT

Building a CHAP Login System: Coding Server-Side Random Seeds
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 14
    2005-09-06

    Table of Contents:
  • Building a CHAP Login System: Coding Server-Side Random Seeds
  • Stepping back: a quick look at the previous CHAP login system
  • Moving forward: Improving the random seed generator
  • Refactoring the CHAP login system: Adding functionality to the JavaScript program

  • 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


    Building a CHAP Login System: Coding Server-Side Random Seeds - Moving forward: Improving the random seed generator


    (Page 3 of 4 )

    As a matter of fact, the random seed generator written to work with the prior example is pretty poor to include in real web applications. It’s precisely for that reason that I’ll define, first, a PHP function aimed specifically at generating true random strings, then another session-wrapping function, tasked with registering session variables, and finally a third function for obtaining their values. The combination of these functions will be the core engine for generating random challenge values, used at a later time by the JavaScript program.

    Having provided an introduction to these functions, here is the definition for the “getRandomString()” function:

    function getRandomString($length=40){

      if(!is_int($length)||$length<1){

        trigger_error('Invalid length for random string');

        exit();

      }

      $chars=
    "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

      $randstring='';

      $maxvalue=strlen($chars)-1;

      for($i=0;$i<$length;$i++){

        $randstring.=substr($chars,rand(0,$maxvalue),1);

      }

      return $randstring;

    }

    Essentially, what this function does is generate a random string of a given length, which will be used as the challenge string sent by the server. At first glance, you can see that its logic is fairly simple to grasp, and of course provides the required flexibility to adjust the length of random strings to specific values. For this case, I’ve set up a default string length of 40 characters.

    Now, let’s pay attention to the next function, “setChallengeVar()”, which acts as a wrapper for registering session variables and assigning random strings to them. It looks like this:

    function setChallengeVar($name='challenge'){

      if(!is_string($name)||!$name){

        trigger_error('Invalid variable name');

        exit();

      }

      session_start();

      // register session variable

      $_SESSION[$name]=getRandomString();

    }

    With reference to the above function, it merely registers a new session variable specified by the $name key and assigns to it the random string returned by the “getRandomString()” function. Since the function is extremely simple, I won’t stop you long with boring details. Instead, I’ll show the next “getSessionVar()” function, which returns a specified session variable. Its source code is the following:

    function getChallengeVar($name='challenge'){

      if(!$_SESSION[$name]){

        trigger_error('Invalid variable name');

        exit();

      }

      return $_SESSION[$name];

    }

    Now, I’m able to obtain a server-side challenge string, by invoking the “setChallengeVar()” function, like this:

    setChallengeVar();

    This simple one-liner registers a “challenge” session variable, and populates it with a 40-digit random string, which will be used as the challenge value by the JavaScript code.

    The next thing to explain is how the JavaScript program integrates the power of the above-listed functions, in order to boost the login system.

    More JavaScript Articles
    More By Alejandro Gervasio


       · The second part of the tutorial looks at several methods to code server-side random...
     

    JAVASCRIPT ARTICLES

    - Building Dynamic Shadows with JavaScript and...
    - Active Client Pages: Chrys`s Approach
    - The Script Approach to Active Client Pages: ...
    - Principles of Active Client Pages: the Scrip...
    - Active Client Pages: the Script Approach
    - Building an RTF-capable Form with the Ext JS...
    - Creating a Multi-Tabbed Online Form with the...
    - jQuery Overview
    - Constructing a Multi-Column Online Form with...
    - Grouping Field Sets on Dynamic Web Forms wit...
    - Building Dynamic Web Forms with the Ext JS F...
    - More on JavaScript Array Objects
    - Methods of the DOM Location Object
    - The DOM Location Object Properties
    - Handling Remote Files with JavaScript Click ...







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