PHP
  Home arrow PHP arrow Page 4 - Adding Users for a Project 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

Adding Users for a Project Management Application
By: David Web
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 1
    2008-08-11


    Table of Contents:
  • Adding Users for a Project Management Application
  • The Script
  • Script Explained
  • Checkformat Function

  • 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


    Adding Users for a Project Management Application - Checkformat Function
    ( Page 4 of 4 )


    To verify that the form value that has been entered follows this pattern, I used a function from the functions file that is called checkformat(). It has the following code: 

    function checkformat($aUsername){

    if(eregi('^[a-z]+[.]+[a-z]+$',$aUsername))

    return TRUE;

    else

    return FALSE;

    }


    The function itself is very simple, and effective. It makes use of regex to match a pattern with the entered string. I put it in a function because I will be using this code in more than one script. Instead of rewriting the code over and over, it is easier to just put it in a function and call it as and when necessary. The email address that we received from the form is checked and verified by the checkmailformat() function. The function has the following code:


    function checkmailformat($aEmail){

    if(eregi('^[a-zA-Z0-9_-.]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+ $',$aEmail))

    return TRUE;

    else

    return FALSE;

    }//end function


    The checkmailformat() function is not very different from the checkformat() function. The only difference is that it has more characters to match, because an email address has one compulsory dot(.) and 'at' (@) character.

    In both checks, the $err and $errmsg variables are appropriately set. And based on these settings (the $err boolean value in particular) the next code is executed:

    //if there is no errors above, then clean the form values before using in query.

    if(!$err){

    //clean vars before inserting into database

    $cuname = mysql_escape_string($_POST['uname']);

    $cupass = mysql_escape_string($_POST['upass']);

    $cname = mysql_escape_string($_POST['fname']);

    $csname = mysql_escape_string($_POST['sname']);

    $cemail = mysql_escape_string($_POST['email']);

    $clevel = mysql_escape_string($_POST['level']);

    The code above continues the checking and cleaning of the form values. This time we escape the form values and transfer them to new variables, all of whom start with a "c." The "c" indicates that the form value has been put through the "cleaner" and is ready to be used in any MySQL query.

    Just renaming the form values will not stop any SQL inject attacks or any other kind of attack. Though not entirely safe, using the mysql_escape_string() function makes things a lot more difficult for any attacker. So please make sure to use this function before running a MySQL query. Since there are no errors, the MySQL query is run to insert the new user details:

    //insert the data

    $query = "INSERT INTO users SET name='" .trim(addslashes($cname)) . "',";

    $query .= "sname='" .trim(addslashes($csname)). "', uname= '" .trim(addslashes($cuname)). "',";

    $query .= "upass='" .trim(addslashes($cupass)). "', level= '" .trim(addslashes($clevel)). "',";

    $query .= "email='" .trim(addslashes($cemail)) . "',last_login='" .trim(addslashes($td)). "'";

    $result=mysql_query($query);

    if(!$result){

    echo mysql_error();

    For debugging purposes I've included the "echo mysql_error()" line. If you are going to use this application in a production environment, please remove that line of code, as it can cause a security vulnerability, in the way of showing too much information. After the user details are inserted into the database, effectively creating the user, the code sends out an email to the user that contains the users log in details. The code below demonstrates how this is done:

    /*email password to user


    //this text will appear in the subject line of the email

    $subject = "Project Management - New User Registration";

    //this is the recipient of the email

    $to = $cleanemail;

    //sender name

    $from_name = "Project Management Application";

    //sender address

    $from_email = "website@mywebsite.com";

    $headers = "From: " . $from_name . " <" . $from_email . ">";

    //build message

    $msg = "Dear ".$csname."<br>";

    $msg .="<br>";

    $msg .= "Below is your new username and password:<br>";

    $msg .= "Username: ".$cuname."<br>";

    $msg .= "Password:".$cupass."<br>";

    $msg .= "<br>";

    $msg .= "Thank you for joining"

    $msg .= "<br>";

    $msg .= "The Management";


    mail($to, $subject, $msg, $headers);




    */


    We use PHP's mail() function to sent the email message. You'll notice that I've commented out this section of the code. This is because it is optional to send the email to the user. Usually when a new employee joins a company, he or she is given the username and password right there and then. So you might want to take the same approach. Or you can choose to send an email to the user as I'm doing here. After sending the email, the code redirects the user to the list_users.php page, that lists all the user of the application:

    header("location:list_users.php");

    }

    }

    }//end submit

    Conclusion

    In the next article we will continue to look at the HTML portion of the user creation script and also take a further look at the last part in the user authentication section that involves password management.



     
     
    >>> 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 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek