PHP
  Home arrow PHP arrow Page 5 - Time is Money (part 1)
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

Time is Money (part 1)
By: The Disenchanted Developer, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 5
    2001-10-22


    Table of Contents:
  • Time is Money (part 1)
  • Up A Creek
  • Bills, Bills, Bills
  • So Many Tables, So Little Time
  • Open Sesame
  • The Lazy Programmer Strikes Again
  • Today's Menu
  • Too Much Information
  • Time For Bed

  • 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


    Time is Money (part 1) - Open Sesame
    ( Page 5 of 9 )

    With the database designed and out of the way, it's time to actually start writing some code. First up, the user login process, and the scripts which verify the user's password and grant him access to the system.

    Here's the initial login form, "index.html".

    <table border="0" cellspacing="5" cellpadding="5"> <form action="login.php" method="post"> <tr> <td>Username</td> <td><input type="Text" name="frmuser" size="15"></td> </tr> <tr> <td>Password</td> <td><input type="password" name="frmpass" size="15"></td> </tr> <tr> <td colspan="2" align="CENTER"><input type="Submit" name="submit" value="Enter"></td> </tr> </form> </table>
    Here's what it looks like:



    Once the form is submitted, the data is processed by "login.php", which connects to the database to verify the username and password against the "users" table.

    <? // login.php - verifies user login // includes include("config.php"); include("functions.php"); // check login and password // connect and execute query $connection = mysql_connect($hostname, $user, $pass) or die ("Unable to connect!"); $query = "SELECT uid, uperms from users WHERE uname = '$frmuser' AND upass = PASSWORD('$frmpass')"; $result = mysql_db_query($database, $query, $connection) or die ("Error in query: $query. " . mysql_error()); // if row exists - login/pass is correct if (mysql_num_rows($result) == 1) { // initiate a session session_start(); // register the user's ID and permission level session_register("SESSION_UID"); session_register("SESSION_UPERMS"); list($uid, $uperms) = mysql_fetch_row($result); $SESSION_UID = $uid; $SESSION_UPERMS = $uperms; // redirect to main menu page header("Location:menu.php"); mysql_free_result ($result); // close connection mysql_close($connection); } else // login/pass check failed { mysql_free_result ($result); mysql_close($connection); // redirect to error page header("Location: error.php?ec=0"); exit; } ?>
    Assuming the username and password is correct, the script initiates a session, and registers two session variables, $SESSION_UID (which contains the user's ID) and $SESSION_UPERMS (which contains the user's permission level). These variables will remain available throughout the session, and will be used in many of the subsequent scripts. The script then redirects the browser to "menu.php", which sets up the main menu for the system, via an HTTP header.

    A login failure will redirect the browser to the generic error handler, "error.php", with an error code indicating the type of error. I'll be using this error handler extensively, to handle the different types of errors possible.

    It is important to note that calls to header() and session_start() must take place before *any* output is sent to the browser. Even something as minor as whitespace or a carriage return outside the PHP tags can cause these calls to barf all over your script.

    Finally, the include()d files, "config.php" and "functions.php", contain variables and functions which will be used throughout the application. The most important of these are the database name, user name and password, which are stored in "config.php" - take a look:

    <? // config.php - useful variables/functions // database parameters // alter this as per your configuration $database="timesheet"; $user = "time_agent"; $pass = "gs645kaf"; $hostname = "localhost"; ?>


     
     
    >>> More PHP Articles          >>> More By The Disenchanted Developer, (c) Melonfire
     

       

    PHP ARTICLES

    - Using Directory Iterators to Build Loader Ap...
    - Using the spl_autoload() Functions to Build ...
    - Working Out of the Object Context to Build L...
    - Using the _autoload() Magic Function to Buil...
    - The Destruct Magic Function in PHP 5
    - The Autoload Magic Function in PHP 5
    - Developing a Recursive Loading Class for Loa...
    - The Sleep and Wakeup Magic Functions in PHP 5
    - Using the Clone Magic Function in PHP 5
    - Including Files Recursively with Loader Appl...
    - The Call Magic Function in PHP 5
    - Designing a Captcha System with PHP and MySQL
    - Using Static Methods to Build Loader Apps in...
    - The Isset and Unset Magic Functions in PHP 5
    - Advanced PHP Form Input Validation to Check ...





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