JavaScript
  Home arrow JavaScript arrow Page 3 - Building a CHAP Login System: An Object-Or...
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: An Object-Oriented Approach
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 10
    2005-09-12

    Table of Contents:
  • Building a CHAP Login System: An Object-Oriented Approach
  • Using a procedural script: a quick overview of the previous login system
  • Taking the object-oriented approach: using a session handling class
  • Putting the pieces together: integrating the “ChallengeGenerator” class

  • 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: An Object-Oriented Approach - Taking the object-oriented approach: using a session handling class


    (Page 3 of 4 )

    To clarify things, allow me to explain the function of the PHP session handling class. Essentially, this class will encapsulate all the required code to registering-deregistering “challenge” session variables, in conjunction with onother relevant tasks such as assigning random values to them and cleaning up complete sessions.

    Since the class’ functionality will be best understood by example, here is its definition:

    class ChallengeGenerator{
      // constructor
      function ChallengeGenerator($clearSession=true){
        if($clearSession){
          $this->clearVars();
        }
        session_start();
      }
      // public method clearVars()
      function clearVars(){
        // destroy existing session
        session_start();
        session_unset();
        session_destroy();
      }
      // public method setChallengeVar()
      function setChallengeVar($name='challenge'){
        if(!is_string($name)||!$name){
          trigger_error('Invalid variable name');
          exit();
        }
        // register session variable
        $_SESSION[$name]=$this->getRandomString();
      }
      // public method getSessionVar()
      function getChallengeVar($name){
        if(!$_SESSION[$name]){
          trigger_error('Invalid variable name');
          exit();
        }
        return $_SESSION[$name];
      }
      function deleteChallengeVar($name){
        if(!$_SESSION[$name]){
          trigger_error('Invalid variable name');
          exit();
        }
        unset($_SESSION[$name]);
      }
      // private method "getRandomString()"
      function getRandomString($length=40){
        if(!is_int($length)||$length<1){
          trigger_error('Invalid length for random string');
          exit();
        }
        $chars=
    "abcdefghijklmnopqrstuvwxyz
    ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $randstring='';
        $maxvalue=strlen($chars)-1;
        for($i=0;$i<$length;$i++){
          $randstring.=substr($chars,rand(0,$maxvalue),1);
        }
        return $randstring;
      }
    }

    Having listed the class, let’s take a look at the definition of each method, for getting a better understanding of their tasks.

    Basically, the constructor cleans up any existing session within the program by calling the “clearVars()” method, unless the $clearSession argument is passed in with a “false” value. Next, the “setChallengeVar()”, “getChallengeVar()” and “deleteChallengeVar()” methods are aimed specifically at performing common session operations, such as registering, obtaining and deleting session variables. This is clearly seen through their respective definitions.

    Additionally, whenever the “setChallengeVar()” method is called, it assigns a random string to the given variable, which is quite useful for easily setting up an object-based random seed. As you can see, I’ve simply wrapped most of the functions reviewed in the procedural script, in order to hide all the internal processing for obtaining server-side challenge strings.

    Considering the class, setting up a random generator is as simple as this:

    // instantiate a ChallengeGenerator object
    $chlgen=&new ChallengeGenerator();
    // register challenge variable
    $chlgen->setChallengeVar();

    As you can see, this is much simpler to code and read.

    With all the class definition done, there remain only a few tasks to be completed. The next step will consist of integrating the class into the previous CHAP script, so you’re able to work with an object-oriented server mechanism for quick generation of challenge values.

    More JavaScript Articles
    More By Alejandro Gervasio


       · The final part of the series looks at an object-based approach to generate...
       · A very interesting article, (as far as it goes!)I have recently developed a...
       · Thank you for commenting on my article. Now, with reference to your problem, make...
       · Since my comment yesterday where I said I was having a problem with the results of...
       · Thank you for your reply. Also, I'm glad to know that you already solved the...
     

    JAVASCRIPT ARTICLES

    - Comparing Fields and Customizing Error Messa...
    - Checking Numbers and File Extensions with jQ...
    - Validating Digits and Dates with jQuery`s Va...
    - Validating Ranges, Emails, and URLs with jQu...
    - More Uses for the jQuery Tooltip Plug-in`s b...
    - Building Image-Based Tooltips with the jQuer...
    - Using the jQuery Tooltip Plug-in`s bodyHandl...
    - Using Rangelength, Min and Max with the Vali...
    - Using Minlength and Maxlength with the Valid...
    - Modifying Tooltip Coordinates with the jQuer...
    - Applying a Fade Out Effect with the jQuery T...
    - Tracking Mouse Movements with the jQuery Too...
    - Checking Online Forms with the Validator jQu...
    - Nested JavaScript Functions as Objects
    - The jQuery Tooltip Plug-in







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek