How To Create Login & Logout Using MySQL Database And PHP Script

Share:

How To Create Login & Logout Using MySQL Database And PHP Script. In the previous tutorial, we have discussed how to create a login and logout page without a MySQL database server using php script. Still with the same topic, we will try to create a login and logout page. However different, this time we will use a MySQL database table to store user data and parsing each login that happens. If the username and password in the login form is loaded the same as in a MySQL database table then the login process will be successful. You want to know, how to create login and logout page using a MySQL database and PHP script ? Please read the tutorial below …
How To Create Login & Logout Using MySQL Database And PHP Script

How To Create Login & Logout Using MySQL Database And PHP Script

Download Now How To Create Login & Logout Using MySQL Database And PHP Script

Create A MySQL Database Table
First, we create a table in the MySQL database server. Here we will give the name of the table with the user and for the name of the database is databasename.

CREATE TABLE 'user' (
  'id' int(11) NOT NULL auto_increment,
  'username' varchar(25) NOT NULL,
  'password' varchar(25) NOT NULL,
  PRIMARY KEY  ('id')
) ;

Create A Login Form
The next step is to create a login form. Simple login form will we use to go to a special page that can only be opened through the login process. Here is the html code to create a login form :
login-form.php

<html>
<head><title> Login Form | How To Create Login & Logout Using MySQL Database And PHP Script </title></head>
<body>
<form action="login-script.php" method="post">
<table>
<tr>
<td>Username</td>
<td><input type="text" name="username" size="20"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password" size="20"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" name="Login" value="Proses"></td>
</tr>
</table>
</form>
</body>
</html>

Create A Login Script
After you successfully create a MySQL database table and create a login form page, next is creating a php script that we will use for the login process. This is the complete php script :
login-script.php

<?php session_start(); 
//load database connection
include("database-connect.php");
$username=$_POST['username'];
$password=$_POST['password'];
// MySQL data
$_SESSION['username']=$username;
$query = $pdo->prepare("select * from user where username LIKE '%$username%' AND password LIKE '%$password%'  LIMIT 0 , 1 ");
$query->execute();
if($check = $query->fetch()){
if($check['username'] == $_POST['username']){
header('Location: user.php');
}else{
echo "You have failed to login. Please <a href='login-form.php'>Login </a> again";
}
} else {
echo "username and password invalid ! Please <a href='login-form.php'>Login</a> again";
}
?>

Create A Connect Page
In order to process the login script and read user tables that exist in the MySQL database server. We have to connect to the database server and the following is a page to connect to the MySQL database server. Here we use PHP PDO.
database-connect.php

<?php
    $host = "localhost";
    $user = "root";
    $password = "";
    $database_name = "databasename";
    $pdo = new PDO("mysql:host=$host;dbname=$database_name", $user, $password, array(
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
    ));
?>

Create A User / Member Page
Next, you can create whatever pages you want, where pages can only be accessed when a person is logged. An example, we create two pages that is user.php and setting.php
Here’s a simple script of the pages :
user.php

<?php session_start();
if(ISSET($_SESSION['username'])){
?>
<h1>This is a user page</h1>
Go to <a href='setting.php'>Setting</a> | <a href="logout-script.php">Logout</a> | <a href="http://tutorial.world.edu/website-programming/how-to-create-login-logout-page-using-mysql-database-php-script/">Tutorial</a>
<?php
}else{
?>This page just for registered users. You are not authorized to access this page. Please <a href="login-form.php">Login</a> first<?php
}
?>

setting.php

<?php session_start();
if(ISSET($_SESSION['username'])){
?>
<h1>This is a setting page</h1>
Go to <a href='user.php'>User</a> | <a href="logout-script.php">Logout</a> | <a href="http://tutorial.world.edu/website-programming/how-to-create-login-logout-page-using-mysql-database-php-script/">Tutorial</a>
<?php
}else{
?>This page just for registered users. You are not authorized to access this page. Please <a href="login-form.php">Login</a> first<?php
}
?>

Note: Give a link to logout page for each this secret pages. So, anytime someone can do a logout process with ease.

Create A Logout Page
The final step is to create a logout page, use this script :
logout-script.php

<?php session_start();
session_destroy(); 
echo "You have successfully logged out!";
echo "<br/>Are you want to login ? Click here : <a href='login-form.php'>Login Form</a>";
echo "<br/><a href='http://tutorial.world.edu/website-programming/how-to-create-login-logout-page-using-mysql-database-php-script/'>Tutorial</a>";
?>

Please try this tutorial, first done in localhost server. Login and logout scripts can you develop and you can even create their own admin page for your website. Good luck 🙂
Thanks for reading How To Create Login & Logout Using MySQL Database And PHP Script

[Tutorial.World.Edu]

One thought on “How To Create Login & Logout Using MySQL Database And PHP Script

Leave a Reply

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