PHP
  Home arrow PHP arrow Page 3 - Authentication Scripts for a User Management Application
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
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? 
Google.com  
PHP

Authentication Scripts for a User Management Application
By: David Web
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 7
    2008-12-01


    Table of Contents:
  • Authentication Scripts for a User Management Application
  • Script Explained
  • Function Continued
  • The logout script

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    Authentication Scripts for a User Management Application - Function Continued
    ( Page 3 of 4 )


    Now, the function starts by defining the chars that are going to be used to generate the new password:

    $chars = "1234567890abcdefGHIJKLMNOPQRSTUVWxyz
    ABCDEFghijklmnopqrstuvwXYZ1234567890";

    Then it sets the $thepass to empty. This variable is going to be used to store the newly generated password, as you will see in a short while:


    $thepass = '';


    Then we come to the heart of the function. A for loop is run and random characters stored in the $chars variable are added to the $thepass variable with each iteration until the count reaches seven:


    for($i=0;$i<7;$i++)

    {

    $thepass .= $chars{rand() % 39};

    }

    Once the loop reaches seven, the function returns the newly generated password:


    return $thepass;

    }


    The final function in the include file is responsible for checking to see if a given email address is correctly formatted. It takes one parameter, which is the email address, and then tests it using regular expressions. This function will be used by any script that requires the user to enter an email address. It has the following code:


    function checkEmail($email){

    if(eregi('^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+.[a-z]{2,8}$',$email)){

    return TRUE;

    }else{

    return FALSE;

    }

    }


    It is used in the following way:


    $Aemail = “jamespayne@webmail.com”;

    if(checkEmail($Aemail)){

    //Email address is correctly formatted do what ever

    }else{

    //set an error message

    }


    The function uses the eregi() function to find out if a given email address is correctly formatted:


    if(eregi('^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+.[a-z]{2,8}$',$email)){


    Let's break this line down. We know that an email address contains an [at] @ sign and that it contains a period [.] and three characters after the period. So in the regular expression above, the first half of the code before the at [@] sign checks to see if the email address contains letters, numbers and any other allowed characters:


    ^[[:alnum:]][a-z0-9_.-]*@

     The next part of the line then checks to see if the email address contains the allowed three characters plus a period [.], which normally appears after the at [@] sign:

    [a-z0-9.-]+.[a-z]

    The final part of the check simply indicates that the final part of the email address before the period, but after the [@] sign can be anything between two and eight characters long; the {2,8} indicates this, and then the value that is to be checked is given (which in this case is $email):

    {2,8}$',$email))


    If the email address evaluates to true, then the function returns true.


    return TRUE;


    Otherwise, it returns false:


    return FALSE;


    The last of the application-wide scripts is the global.php script. This script contains the database connection details and is made available to any script that needs to access the database. It has the following code:


    <?php

    session_start();

    $host ="localhost";

    $pw ="mypass";

    $user = "myuser";

    $dbname = "user";

    $dbc=mysql_connect($host,$user,$pw) or die(mysql_error());

    mysql_select_db($dbname) or die(mysql_error());

    ?>


    The script is fairly easy to understand. It basically just fills some variables with the connection details that the database server requires to make a successful connection. At all stages of the connection attempt, an appropriate error message will be displayed if it occurs:


    $dbc=mysql_connect($host,$user,$pw) or die(mysql_error());

    mysql_select_db($dbname) or die(mysql_error());




     
     
    >>> More PHP Articles          >>> More By David Web
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





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