Miscellaneous

  Home arrow Miscellaneous arrow Page 4 - Writing a Basic Authentication System ...
MISCELLANEOUS

Writing a Basic Authentication System in PHP
By: bluephoenix
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 18
    2004-12-24

    Table of Contents:
  • Writing a Basic Authentication System in PHP
  • Storing Passwords
  • Getting the User Login
  • Processing the Login
  • Persisting the Authentication
  • Conclusion

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    Writing a Basic Authentication System in PHP - Processing the Login


    (Page 4 of 6 )

    The actual authentication can take place once we have the user's login ID and password. We need to encrypt or hash the password the same way it was initially done.

    $user = $_POST["userid"];
    $pass = sha1($_POST["password"]);

    With the ID and password value, we can query the database for any matching records. The following SQL statement is designed to return records where the login ID and password hash match.

    SELECT * FROM  Users WHERE User = '$user' AND Password = '$pass'

    If the query returns a record set then the login credentials are valid and the user may have access to the protected information. If the query fails to return a record then the credentials are invalid and access is denied.

    The validate.php script that would accomplish all of that might resemble the following:

    <?php
    /* get the incoming ID and password hash */
    $user $_POST["userid"];
    $pass sha1($_POST["password"]);

    /* establish a connection with the database */
    $server mysql_connect("localhost""mysql_user",
              
    "mysql_password");
    if (!
    $server) die(mysql_error());
    mysql_select_db("myDatabase");
      
    /* SQL statement to query the database */
    $query "SELECT * FROM Users WHERE User = '$user'
             AND Password = '$pass'"
    ;

    /* query the database */
    $result mysql_query($query);

    /* Allow access if a matching record was found, else deny access. */
    if (mysql_fetch_row($result))
      echo 
    "Access Granted: Welcome, $user!";
    else
      echo 
    "Access Denied: Invalid Credentials.";

    mysql_close($server);  
    ?>

    Instead of simply echoing "Access Granted" or "Access Denied" as shown here, your script can set cookies or start sessions, redirect the user to the login form or perform whatever else is needed.

    More Miscellaneous Articles
    More By bluephoenix

    blog comments powered by Disqus

    MISCELLANEOUS ARTICLES

    - Oracle Database XE: Indexes and Sequences
    - Modifying Tables in Oracle Database XE
    - Oracle Database XE: Tables and Constraints
    - More on Oracle Databases and Datatypes
    - Oracle Database XE Datatypes: Datetime and L...
    - Oracle Database XE Datatypes: Character and ...
    - From Databases to Datatypes
    - Firefox 3.6.6 Released with Improved Plug-in...
    - Attention Bloggers: WordPress 3.0 Now Releas...
    - Reflection in PHP 5
    - Inheritance and Other Advanced OOP Features
    - Advanced OOP Features
    - Linux from Scratch V.6.6 Review
    - Linux Gaining in Strength
    - Install Slackware on Your Old PC


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 10 - Follow our Sitemap