Code2Design.com

User login

The Layout

Programming

Graphic Design

Resources

Navigation

C2D Projects

Unsystematic Affiliates

Adobe Tutorialz Spoono Tutorial-Search Ray-Industries 

Change Language

Who's online

There are currently 0 users and 8 guests online.

New Login Form

This isn't a 100% bullet-proof script. It is just to show the basic idea of how these things work. Later on I will be posting my own extensive script that will be more suited to a live environment.

Here is a revision I did of this script:

<?php

   
//Include the database connection
   
include "data.php";
   
   
//In order to work with sessions we need use session_start()
   
session_start();
   

   
//Return true if the session is set
   
function is_logged_in(){
        return isset(
$_SESSION['loggedIn']);
    }
   
   
//Check to see if they posted a value called "login"
   
function is_logging_in(){
        return isset(
$_POST['submit']);
    }
   
   
//Function to show the login form
   
function loginForm(){ 
        print 
'
        <form method="post">
        <strong>Username:</strong> <input type="text" name="username" /><br />
        <strong>Password:</strong> <input type="password" name="password" /><br />
        <input type="submit" name="submit" value="Login" />
        </form>'
;
    }

   
//See if the login matches a user in the database
   
function login($username$password){
       
       
//Clean the values of XSS and Injections
       
$username trim(htmlentities(strip_tags($username), ENT_QUOTES'UTF-8'));
       
$password md5(trim(htmlentities(strip_tags($password), ENT_QUOTES'UTF-8')));
       
       
//Create the MySQL Query
       
$query 'SELECT * FROM `registered` WHERE `username` = \''.mysql_real_escape_string($username). '\' AND password = \''mysql_real_escape_string($password). '\'';
       
$result mysql_query($query);
       
       
//If we found 1 or more users that matched the login
       
if(mysql_num_rows($result) > 0) {
       
           
$_SESSION['loggedIn'] = true;
           
header("Location: "$_SERVER['PHP_SELF']);
            exit;
           
        } else {
            echo 
'<strong>Bad login!</strong><br />';
           
loginForm(); //here we ask the user to login again...
           
exit;
        }
       
    }
   
   
//Print "You are loged in" and end the script
   
function loggedIn(){
        die(
'<h1>You are loged in!</h1>');
    }
   
   
   
//Here comes the logic...
   
    //If they are already loged in
   
if(is_logged_in()){
       
loggedIn();
   
   
//Else if they have submited the form to login 
   
} elseif(is_logging_in()){
       
login($_POST['username'], $_POST['password']);
   
   
//Else this must be the first time they have come so show the login page
   
} else {
       
loginForm();
    }
?>


Reply

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <br> <br /> <h3>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use BBCode tags in the text, URLs will be automatically converted to links
More information about formatting options



Like what you see?

Why not add more? C2D is looking for other Christian Web Masters who would like to help write articles for this site. If you have expericance in FLASH, CSS/HTML, PHP/MySQL, PhotoShop/GIMP, Blender, Javascript, or just General Design - our users would love to hear what you have to say. Contact Us

delicious   digg   reddit   magnoliacom   newsvine   furl   google   yahoo   technorati