PHP
  Home arrow PHP arrow Page 2 - Project Management: Administration
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: Administration
By: David Web
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 1
    2008-08-25


    Table of Contents:
  • Project Management: Administration
  • The Admin Login Script
  • The Index Page
  • The Other Scripts

  • 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: Administration - The Admin Login Script
    ( Page 2 of 4 )

    When a user uses the project management application, they have to log in to be given access to the application. This access will be granted if the user is in the database. The code for this verification process is something like this:


    <?php

    include "dbcon.php";

    include "functions.php";

    //initialise variables

    $err="";

    $errmsg=false;


    //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


    ?>

    This is verification stage one. The important part in this code is the one listed below:

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

    Take a closer look at where the authentication process successfully verifies the user. It is at this point that the user's details are transferred into session variables, i.e:

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

    These are the login variables that the secondary admin login script uses to determine if a user has the right to access the admin section:

    <?php

    ob_start();

    session_start();

    if(isset($_SESSION['level'])){

    $level = $_SESSION['level'];

    //if the access level is admin, then grant access to user

    if($level == "admin"){

    header("location:index.php");

    As you can see from the above code snippet, the user access level information is stored in the $_SESSION['level'], which is then transferred to a local variable called "$level."

    $level = $_SESSION['level'];

    The value of the local variable is then compared against a string called "admin":

    if($level == "admin"){

    If the value that is contained in the $level variable is admin, then the user is granted access to the admin section. Otherwise the user is redirected to the main login page:

    }else{//level does not contain admin, redirect user to login page

    header("location:../login.php");

    }

    If the session variable is not set, it means that this user is trying to access the page without going through any of the login checks that are required. The script simply redirects them to the main login page:

    }else{

    //send user to login

    header("location:../login.php");


    }//end session check


    ob_end_flush();

    ?>

    The ob_end_flush() function is then used to "flush" out any unsent headers to avoid the "headers already sent" error message.



     
     
    >>> 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