Home Forum Search Contact Us Advertise
Tutorial

Login form

Thumbnail

by KiNgY | in PHP | posted December 20, 2007

Rating starDefault starDefault starDefault starDefault star (1 vote) | 542 views

This tutorial should make you understand how a simple login script works

Add to del.icio.us | Digg! Digg this | Dot This

You must login in order to rate this.
Hey so this is my first tutorial, this isn't a step by step tutorial as you can see i have just wrote a login script and explained what i was doing as i go along if it is a success i will write a register tutorial as well.

Thanks KiNgY

PHP:


<?php 
    session_start
();
    include(
"includes/db_connect.php");
?>


The session_start(); function is important and needs to go at the start of each page you want logged in members to access.

The include() function just includes my db_connect script which allows me access to my database.

PHP:


<?php 
if($_POST['login']!='') { // tells php what to do if the login button is pressed
    
    // set login variables
    
    
$user mysql_real_escape_string($_POST['user']);
    
$pass mysql_real_escape_string($_POST['pass']);
    
    
// check critical login data has been submitted
    
if($user=='' && $pass==''){
    
    
$id "1"// id number for displaying certain errors
    
}elseif($user=='') {
    
    
$id "2"// id number for displaying certain errors
    
}elseif($pass=='') {
    
    
$id "3"// id number for displaying certain errors
    
}else {
    
    
//  create the deciding data
    
    
$query "SELECT * FROM users WHERE username = '$user' && password = '$pass'";
    
$result mysql_query($query);
    
$decide mysql_num_rows($result); // brings back the amount of rows with the username and password submitted
    
    // check if data is correct
    
    
if($decide=='1') {
        
$_SESSION['username']="$user";  // sets the session variable username
        
Header("Location: logged_in.php"); // sends the user to the specified page logged_in.php
        
exit;
    }else {
        
$id "5"// id number for displaying certain errors
    
}
}
}
?>


This i feel is rather self explanatory, the value of $id depends on which fields of data are missing, i will use $id further down the script to display certain error messages.

If the required data is entered it will be queried against my database and if there is a row with username $user and password $pass the login is a success and the session username is set and the user is forwarded onto a different page in this case logged_in.php.

HTML for the login form
there is also some php in here for displaying error messages


<form action="login.php" method="POST" name="login">

<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td colspan="2" align="center">User Login</td>
</tr>

PHP:


<?php 
//this is where the id number comes in to display certain error messages.
    
if($id>'' && $id<='3'){ echo "<tr><td colspan='2' align='center'>Please fill in missing fields (<span color='red'>*</span>)</td></tr>"; }

if(
$id=='5'){ echo "<tr><td colspan='2' align='center'>Sorry but your login details where incorrect</td></tr>"; }
?>


<tr>
<td align="left">

PHP:


<?php if($id=='1' || $id=='2'){ echo "<span color='red'>*</span> "; }?>


Username: </td><td><input type="text" name="user"></td>
</tr>
<tr>
<td align="left">

PHP:


<?php if($id=='1' || $id=='3'){ echo "<span color='red'>*</span> "; }?>


Password: </td><td><input type="password" name="pass"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="login" value="login"></td>
</tr>
</table>

</form>

Links

If you would like to see the completed code please visit here:

Full source code

Thanks again KiNgY