Create Simple Login & Logout Page Without MySQL Database Using PHP Script

Share:

Create Simple Login & Logout Page Without MySQL Database Using PHP Script. Do you want to create a special page that can be accessed only through a login form? in this tutorial, we will discuss about how to create a login and logout page using a php script. Usually, most of the websites will require a connection to the MySQL database to call and match the username and password user but in this tutorial we will create a simple login and logout page without MySQL database. Just go ahead, read the tutorial below. Read more
Create Simple Login & Logout Page Without MySQL Database Using PHP Script

Create Simple Login & Logout Page Without MySQL Database Using PHP Script

Download Now Create Simple Login & Logout Page Without MySQL Database Using PHP Script

First, we must create a login page. Put, session_start () on the page. Furthermore, we must create a php array to store the username and password. To protect websites from hacker attacks, do not forget the password encrypted using md5 string. Then, you save the results in an array. For example :

array(
         'username1'=>'7c6a180b36896a0a8c02787eeafb0e4c',
         'username2'=>'6cb75f652a9b52798eb6cf2201057c73',
);

PHP script that is most often used to create a login and logout page is a session function. Here is a php script to check and match the username and password from the login form with array database :

if(isset($_POST['username'])) {
    if($userinfo[$_POST['username']] == md5($_POST['password'])) {
        $_SESSION['username'] = $_POST['username'];
    }else {
        //Invalid Login
		echo "Username or password incorrect!";
    }
}

This script is written only once, namely on the login page and not the admin / member page.
And here is the html script to create a simple login form :

<form name="login" action="" method="post">
Username:  <input type="text" name="username" value="" /><br />
Password:  <input type="password" name="password" value="" /><br />
<input type="submit" name="submit" value="Submit" />
</form>

Moderate session function to do a logout is as follows :

if(isset($_GET['logout'])) {
    $_SESSION['username'] = '';
    header('Location:  ' . $_SERVER['PHP_SELF']);
}

The logout script could be written on every page of the admin / members area so as to logout, the user does not need to visit a special page and logout simply click on the logout link on every admin / member area page. To be safe, preferably after successful logout process, the user is directed to another web page. An example is a redirect the login page as follows :

if(isset($_GET['logout'])) {
    $_SESSION['username'] = '';
    header('Location: login-without-database.php']);
}

Here is the html script to make the logout link :

<a href="?logout=1">Logout</a>

If someone has managed to login, they will see a special phrase that only appear when someone login. On the login page we can create it, that is the content that is confidential, place among the following php script :

<?php if($_SESSION['username']): ?>
Show content if you are already logged in ...
<?php endif; ?>

Or use the following if else in the body tag :

<?php if($_SESSION['username']): ?>
Show content if you are already logged in ...
<?php else: ?>
Other content without the need to login ...
<?php endif; ?>

From the above, it is an example of a simple login and logout pages without MySQL database :
login-without-database.php

<?php
session_start();

$userinfo = array(
                'username1'=>'7c6a180b36896a0a8c02787eeafb0e4c',
                'username2'=>'6cb75f652a9b52798eb6cf2201057c73',
                );
				
if(isset($_POST['username'])) {
    if($userinfo[$_POST['username']] == md5($_POST['password'])) {
        $_SESSION['username'] = $_POST['username'];
    }else {
        //Invalid Login
		echo "Username or password incorrect!";
    }
}				

if(isset($_GET['logout'])) {
    $_SESSION['username'] = '';
    header('Location:  ' . $_SERVER['PHP_SELF']);
}
?>
<html>
    <head>
        <title> Login Page | Create Simple Login & Logout Page Without MySQL Database Using PHP Script </title>
    </head>
    <body>
        <?php if($_SESSION['username']): ?>
            <p>You are logged in as <?=$_SESSION['username']?></p>
            <p><a href="?logout=1">Logout</a></p>
		    <p>Go to <a href="admin-without-database.php">admin</a> page or <a href="http://tutorial.world.edu/web-development/create-simple-login-logout-page-without-mysql-database-php-script/">tutorial</a></p>
        <?php endif; ?>		
        <form name="login" action="" method="post">
            Username:  <input type="text" name="username" value="" /><br />
            Password:  <input type="password" name="password" value="" /><br />
            <input type="submit" name="submit" value="Submit" />
        </form>
    </body>
</html>

admin-without-database.php

<?php
session_start();

if(isset($_GET['logout'])) {
    $_SESSION['username'] = '';
    header('Location: login-without-database.php');
}
?>
<html>
<head>
<title> Admin & Member Area | Create Simple Login & Logout Page Without MySQL Database Using PHP Script </title>
</head>
    <body>
        <?php if($_SESSION['username']): ?>
            <p>Hello, <?=$_SESSION['username']?></p>
            <p><a href="?logout=1">Logout</a></p>
			<p>This is a secret page. Just for a admin! <br/><a href="http://tutorial.world.edu/web-development/create-simple-login-logout-page-without-mysql-database-php-script/">Go to tutorial post</a></p>
	    <?php else: ?>
		<?php
			header('Location: login-without-database.php');
		?>
		<?php endif; ?>
    </body>
</html>

In the admin page, I accidentally making a redirect url if someone tries to access this page without first login on the website so that confidentiality can be maintained and safe from hacker attacks.

Actually, login and logout page without MySQL database is not recommended for use in our website because a lot of shortcomings, including :

  • Password and username are still inadequately protected because only stored in an array function.
  • Difficult to manage because we have to make the encryption password and edit the login page each when our add a new member. Not to mention, if there is a member who wants to change the password, you must do it manually.
  • Sometimes cookies in the browser easily traced, making it easier for someone to get your username and password.

Regardless of any shortcomings in this php script, we can learn a lot. By using a simple php script we can learn to make a simple login and logout page in the localhost computer itself. Good applied because it does not require a long process because it does not need to make calls to the MySQL database server.
Thanks for reading Create Simple Login & Logout Page Without MySQL Database Using PHP Script

[Tutorial.World.Edu]

2 thoughts on “Create Simple Login & Logout Page Without MySQL Database Using PHP Script

Leave a Reply

Your email address will not be published. Required fields are marked *