Tutorials >> View >> 1
Yahoo chat: marklester_aover
Bookmark and Share

Simple PHP Secure Session Login System

This tutorial deals with using Session to have a simple Secured Login system. It has 3 files the main index.php, validate.php and the logout.php, these three is the basic files in creating a simple PHP Secured Login system. Copy the codes or download the zip file to see the result.

Learn the codes below:

<?php

/* index.php */
/*always put the session_start() function above the page tags*/
session_start();
?>
<html>
<head>
<title>Simple PHP Secure Session Login System</title>
<style type="text/css">
* {
    margin: 0;
    font-family: Arial, Helvetica, sans-serif;
}
body {
    text-align: center;
    background-color: #999;
    color: #000;
}
a {
    color: #FFF;
}
h2, form {
    margin-top: 100px;
}
</style>
</head>
<body>
<?php

/*
check if session name username is not set or registered
if not set or registered it will display the Login Form
*/

if(!session_is_registered(username)){
   
?>
    <form method="post" action="validate.php">
    Username: <input type="text" name="username" /><br />
    Password: <input type="password" name="password" /><br />
    <input type="submit" name="login" value="login" />
    </form>
   
<?php
}

/*
check if session name "username" is set or registered
if set or registered it and the "Username" and "Password"
is correct it will redirect here and display the
Login Sucessfull message
*/

if(session_is_registered(username)){
    /*
    $_SESSION['username'] code display or output the
    registered session name "username"
    */

   
?>
    <h2>Login Sucessfull, Welcome...</h2>
    <h4>Username: <?=$_SESSION['username']
?><h4>
    <h4>Password: <?=$_SESSION['password']
?><h4>
    <h4><a href="logout.php">logout</a><h4>
   
<?php
}
?>
</body>
</html>


<?php
/* validate.php */
/*
This function ob_start() will turn output buffering on. While output
buffering is active no output is sent from the script
(other than headers), instead the output is stored in
an internal buffer.
*/

ob_start();

/*
check if Method use is POST if not it will redirect
back to the Login Form
*/

if( $_SERVER['REQUEST_METHOD'] == "POST" ) {
    /*assign post data "username" to $username php variable*/
    $username = $_POST['username'];
    /*assign post data "password" to $username php variable*/
    $password = $_POST['password'];
   
    /*registers the global variable with that name in the current session*/
    session_register("username"); /*register $username php variable*/
    session_register("password"); /*register $password php variable*/
   
    /*
    check if $username and password is equal to Username: user and
    Password: user. If True it will redirect back to "index.php"
    and display the Login Sucessfull message, Username and Password
    */

    if($user == "user" && $password == "user"){
        /*
        extra codes here if you want...
        */

       
        /*redirect back to index.php and display Login Sucessfull message*/
        header("location: index.php");
       
        /*
        ob_end_flush() Flush (send) the output buffer
        and turn OFF output buffering
        */

&nb

Click image to download the Zip file: PHP-Secure-Session-Login-System.zip

# of Comments (2)

by: Rybaxs

all say, "How hard it is that we have to search codes"--a strange complaint to come from the mouths of programmers. nice tutorials - simple but explicit.. common but needed.. :D

2008-06-04
by: Mark Abucayon

Thanks for sharing this us this may help us alot, I wanna try this later. Great Job!

2008-06-04

Add your comment here...

Please feel free to make a comment and suggestion on this tutorial. Your comment and suggestion will help improve the tutorial for the benefit of you and others. Thank you...

Name : *
Email Add : *
URL (include http://):
Messages : *
Security Code : *
(*) Must not leave blank.
captcha

Our Main Services

We develop and deliver the following services:

Latest News...

Search My Site

 This Site   Web

Google Ads

Google Ads

Powered By:

Advertise

Place your Custom Advertise here $29 for 1 month. Image dimension 276x180 pixels. Click the Paypal "Buy Now" button below.