MySQL
  Home arrow MySQL arrow Page 2 - Creating a Login Script for a PHP/MySQL Blogging System
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  
MYSQL

Creating a Login Script for a PHP/MySQL Blogging System
By: Jacques Noah
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 68
    2006-10-03


    Table of Contents:
  • Creating a Login Script for a PHP/MySQL Blogging System
  • Login.php
  • Logout.php
  • Password.php

  • 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


    Creating a Login Script for a PHP/MySQL Blogging System - Login.php
    ( Page 2 of 4 )

    This file displays a form that requests your username and password and also gives you the options to register as a new user or recover your password if you've forgotten it.

     Once you've  pressed the submit button the following code gets executed:

    <?
    session_start();
    if(isset($_GET['reg'])){
    $reg=$_GET['reg'];
    }else{
    $reg="";
    }
    if($reg==1){
    $msg1="<font color="#FF0000"><b>Your details have been added,
    please login</b></font>";
    }elseif($reg==2){
    $msg1="<font color="#FF0000"><b>You have been successfully
    logged out.</b></font>";
    }elseif($reg==3){
    $msg1="<font color="#FF0000"><b>You have been redirected because you need to be logged on as administrator.</b></font>";
    }
    if(isset($_POST['submit'])){
    if( empty($_POST['uname']) && (empty($_POST['upass']))){
    header( "Location:Messages.php?msg=1" );
    exit();
    }
    //transfer to shorter var
    $n=$_POST['uname'];
    $p=$_POST['upass'];
    //connect to db
    include('config.php');
    $query="select * from user where uname='$n' and pw='$p'";
    if($result=mysql_query($query)){
    $row=mysql_fetch_assoc($result);
    //check each var
    if($n !=$row['uname']){
    header( "Location:Messages.php?msg=2" );
    exit();
    }
    if($p !=$row['pw']){
    header( "Location:Messages.php?msg=11" );
    exit();
    }
    if($row['isbanned']=='yes'){
    header( "Location:Messages.php?msg=12" );
    exit();
    }
    }//ifresult
    //put in session vars
    $_SESSION['level'] = $row['level'];
    $_SESSION['status'] = 'logged';
    $_SESSION['username'] = $n;
    //This takes you to the admin pages; change this to take you to
    wherever you want it //to go.
    header("location:../admin/main.php");
    exit;
    }?>

    Ignore the first part that falls under "if(isset($_GET['reg'])){}" since it concerns another script; we will come to it later. The next part does the following things:

    • Checks to see whether a user exists. If so, the username and password is compared with the information in the database. It also checks to see whether the user is banned. If all the checks are okay, the script puts the username in a session variable and then sends the user through to the blog.
    • If the user does not exist, the program goes to the messages page and displays an error message.

    The script also checks the user's banned status. If a user is banned, then the script directs you to the Messages page. The submitted username and password is checked individually and then the appropriate action is taken. This enables the user to know exactly which of the two, username or password, is wrong.

    You will also notice that some session variables are created. These are going to be used by the blog system to identify the user and by the admin script to identify whether a user indeed has admin level clearance.



     
     
    >>> More MySQL Articles          >>> More By Jacques Noah
     

       

    MYSQL ARTICLES

    - MySQL Security Tips
    - Designing a MySQL Database: Tips and Techniq...
    - The Three Most Important MySQL Queries
    - Null and Empty Strings
    - MySQL Server Tuning Tips and Tricks
    - MySQL Query Optimizations and Schema Design
    - MySQL Benchmarking Tools and Utilities
    - MySQL Benchmarking Concepts and Strategies
    - Take Some Load off MySQL with MemCached
    - MySQL Table Prefix Changer Tool in PHP
    - Using the SIGNAL Statement for Error Handling
    - Error Handling Examples
    - Error Handling
    - Completing a Search Engine with MySQL and PH...
    - Paginating Result Sets for a Search Engine B...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek