PHP
  Home arrow PHP arrow Page 2 - 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? 
Google.com  
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 - Create the Table
    ( Page 2 of 4 )


    So create a table with the following SQL:

    CREATE TABLE `users` (

    `uid` int(11) NOT NULL auto_increment,

    `name` varchar(20) NOT NULL default '',

    `sname` varchar(20) NOT NULL default '',

    `uname` varchar(100) NOT NULL default '',

    `upass` varchar(8) NOT NULL default '',

    `level` enum('admin','normal') NOT NULL default 'normal',

    `last_login` datetime NOT NULL default '0000-00-00 00:00:00',

    `email` varchar(100) NOT NULL default '',

    PRIMARY KEY (`uid`)

    ) TYPE=MyISAM AUTO_INCREMENT=5 ;



    Below is some sample data for the table:


    INSERT INTO `users` VALUES (1, 'jack', 'dee', 'jack.dee', 'pass', 'admin', '0000-00-00 00:00:00', 'jack@dee.com');

    INSERT INTO `users` VALUES (2, 'maria', 'garises', 'maria.garises', 'pass', 'normal', '0000-00-00 00:00:00', 'maria@garises.com');

    INSERT INTO `users` VALUES (3, 'kine', 'brand', 'kine.brand', 'pass', 'normal', '0000-00-00 00:00:00', 'kine@brand.com');

    INSERT INTO `users` VALUES (4, 'john', 'doe', 'john.doe', 'pass', 'normal', '0000-00-00 00:00:00', 'john@doe.com');


    Copy and paste the above SQL in your MySQL administration application and run it. You should have a table called "users" with the sample data above. Now, let's create the login script that will run the login process for us. Create a new PHP document and add the following code:


    <?php

    include "dbcon.php";

    include "functions.php";

    //initialise variables

    $err=false;

    $errmsg="";


    //is form submitted?

    if(isset($_POST['submit'])){

    //check that the form values are not empty, if so, set errormsg value

    if(empty($_POST['uname'])){

    $errmsg="The username field is empty, please enter a username<br>";

    $err=true;

    }

    if(empty($_POST['upass'])){

    $err=true;

    $errmsg .="The password field is empty, please enter password<br>";

    }


    //check that the username is in correct format

    if(!checkformat($_POST['uname'])){

    $err=true;

    $errmsg .="The username that you entered has a incorrect format.<br>";

    }



    //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']);


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

    $checkuser_res = mysql_query($checkuser);

    $checkuser_num = mysql_num_rows($checkuser_res);


    if($checkuser_num > 0){

    //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");

    }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


    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/userauth.dwt.php" codeOutsideHTMLIsLocked="false" -->

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

    <!-- InstanceBeginEditable name="doctitle" -->

    <title>Project Management ::Login</title>

    <!-- InstanceEndEditable -->

    <!-- InstanceBeginEditable name="head" --><!-- InstanceEndEditable -->

    <link href="Templates/loginstyle.css" rel="stylesheet" type="text/css" />

    </head>


    <body>

    <table width="100%" border="0">

    <tr>

    <td bgcolor="#6699CC" class="headertxt">Project Management:: User Authentication </td>

    </tr>

    <tr>

    <td><!-- InstanceBeginEditable name="main" -->

    <table width="100%" border="0" class="formborder">

    <tr>

    <td colspan="2" class="loginheader">Login</td>

    </tr>

    <tr>

    <td colspan="2">&nbsp;</td>

    </tr>

    <form action="login.php" method="post" name="f1" class="formborder">

    <?php if(isset($errmsg)){?>

    <tr>

    <td colspan="2" class="errmsg" ><?php echo $errmsg; ?></td>

    </tr>

    <tr>

    <td colspan="2">&nbsp;</td>

    </tr>

    <tr>

    <?php

    }

    ?>

    <td width="10%" valign="bottom"><strong>Username:</strong></td>

    <td width="90%"><label>

    <input name="uname" type="text" class="login" id="uname" size="40" />

    </label></td>

    </tr>

    <tr>

    <td valign="bottom"><strong>Password:</strong></td>

    <td><label>

    <input name="upass" type="password" class="login" id="upass" size="40" />

    </label></td>

    </tr>

    <tr>

    <td>&nbsp;</td>

    <td><a href="password.php">Forgot your password?</a> </td>

    </tr>

    <tr>

    <td>&nbsp;</td>

    <td><label>

    <input name="submit" type="submit" id="submit" value="Log me in!" />

    </label></td>

    </tr>

    </form>

    </table>

    <!-- InstanceEndEditable --></td>

    </tr>

    <tr>

    <td align="right" class="cright">copyright &copy; 2007 PM </td>

    </tr>

    </table>

    </body>

    <!-- InstanceEnd --></html>






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