PHP
  Home arrow PHP arrow Page 4 - Project Management: Authentication
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? 
PHP

Project Management: Authentication
By: David Web
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 3
    2008-07-28


    Table of Contents:
  • Project Management: Authentication
  • Create the Table
  • The Login Code
  • Login Code continued

  • 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


    Project Management: Authentication - Login Code continued
    ( Page 4 of 4 )

    After testing to see if the fields are empty, and verifying the username format, the code goes on to run a check in the database to try to match the username and password entered by the user, after escaping the form data:

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

    if(!$err){

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

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


    As the code above shows, the two form values are escaped using the mysql_escape_string() function. This function gets rid of any white spaces or slashes that may be in the form values, and thus cleans the values for use in a MySQL query. This kind of filtering is very important when running queries, as it closes any security vulnerabilities that may otherwise occur. Next the code runs the MySQL query to actually compare the information from the form with the data in the database:


    $checkuser = "SELECT * from users WHERE uname = '".$cleanuname."' AND upass = '".$cleanupass."'";

    $checkuser_res = mysql_query($checkuser);


    The outcome of the check is stored in the $check_user variable, which will contain a value that is greater than zero if there's a match and a value that is less than zero if there is not a match:


    $checkuser_num = mysql_num_rows($checkuser_res);

    if($checkuser_num > 0){


    The code above checks to see if there is a match by finding out if the value returned by the query is greater than zero. If so, we transfer user details to the session variables. The session has already been started by the inclusion of the dbcon.php file that contains the session_start() function. The name, user id and access level of the user are stored in the session variables, then the user is granted access to the application. From this point on the user will be tracked:


    //if user exists and passes authentication

    //setup session variables and redirect to index page

    $row = mysql_fetch_assoc($checkuser_res);

    $_SESSION['name'] = $row['name']." ".$row['sname'];

    $_SESSION['uid'] = $row['uid'];

    $_SESSION['level'] = $row['level'];


    //redirect

    header("location:main.php");


    If the user details do not match, the $errmsg variable is assigned an error message which is then displayed on the log-in form:


    }else{

    //if values do not match set errmsg

    $err=true;

    $errmsg .="The username or password you entered does not match.<br> MYSQL ERROR ".mysql_error();

    }//else


    }//end $err check


    } //end form submit check


    Conclusion

    The next article will finish off the section on user log-in and log out.



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

       

    PHP ARTICLES

    - 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
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





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