WebDev: Simple Login Tutorial Using PHP. (DB Less Version)

Hi Friends,
Today I would like to say how to create a login system using PHP, without using Databases.

We need these files:
1. index.php - Index file, where your protected content and login form is there.
2. login.php - Login Form.
3. logout.php - Script for logging out.

Code: index.php
php
session_start
(); // Initiate the session
?>
    "https://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">


    
    Welcome
    
        * {font-family: Tahoma; font-size: 12px;}
        a {color: #0000ff; text-decoration: none;}
        a:hover {color: #ff0000;}
    



        if(isset($_SESSION["logged"])) // Check if the user had logged in.
        
echo 'Welcome! [url=logout.php]Logout[/url].';
    else 
// User did not log in.
        
echo 'Please [url=login.php]Login[/url].';
    
?>

Code: login.php
php
session_start
(); // Initiate the session.
$err false// Set an Error Flag.
if(count($_POST))
    if(isset(
$_POST["username"], $_POST["password"])) // See if the user has entered both username and password.
        
if($_POST["username"]=="Admin" && $_POST["password"]=="LetMeIn"// Check for the correctness of both username and password.
        
{
            
$_SESSION["logged"] = true// Set the session as authenticated.
            
header('Location: index.php'); // Redirect the user to the home page.
            
die(); // Stop the script.
        
}
        else
            
$err true// Username or password is wrong! Set error flag.
    
else
        
$err true// User didn't provide both username and password.
else
    
$err false// User logs in for the first time.
?>


    
    Login
    
        * {font-family: Tahoma; font-size: 12px;}
        label {display: inline-block; width: 75px; margin: 5px 0px; padding: 5px;}
        input {border: 0px; border-bottom: 1px solid #cccccc; width: 100px; margin: 5px 0px; padding: 3px;}
        form {text-align: center;}
    



    if($err) echo '[b]Invalid Login! Provide all values![/b]'// If the error flag is set, display error message. ?>
    
        
Username: 

        
Password: 

        

    

Code: index.php
php
    session_start
(); // Initiate the session
    
session_destroy(); // Destroy the session
    
header('Location: index.php'); // Redirect the user to the home page
?>
Now keep all the files in the same folder... Ready to execute?

Execution
I personally recommend USB Web Server for fast food execution. Download it at #-Link-Snipped-#
Now copy the three files in the Root folder of USB Web Server and open the port #-Link-Snipped-# in your favourite browser and enjoy. I have also attached the set of files, which I used with this post. 😀

All the coding is documented. Even then if you have any queries, do shoot them out in the replies. 😀

Download the files: #-Link-Snipped-#

Replies

You are reading an archived discussion.

Related Posts

in pn junction , what s depeletion capactiance and junction capactiance
'Yantra' in the ancient Indian language of Sanskrit, means 'machine', a 'Yantrik' an inventor. Yantrix uses the power of creativity to empower your world with unique machines invented for novel...
I am facing problem in connecting to postgre database in xampp when try to execute the following script It is showing me an unexpected error ie undefined function pg_connect() Here...
My classmates and I had talked about this.. can anyone tell me why the sea is salty??!😕
Volvo's usually a known name in the luxury buses in India. Volvo's entered the high-end car market in India with Volvo S60. The price is Rs. 27,00,000/- (Ex. Showroom, Delhi)....