How to create Login, Registration and Logout application in php?

config.php

create configuration file and set database configuration.

File name : config.php

$host = "localhost";
$username ="ittutorial";
$password = "sana*mahtab";
$db_name = "itechxpert";

DbConnection

create Dbconnection page for database connection object.

File name : dbconnection.php

include 'config.php';
$con = mysqli_connect($host,$username,$password)or die(mysqli_error());
mysqli_select_db($con,$db_name)or die(mysqli_error($con));

// $con = mysqli_connect($host,$username,$password,$db_name)or die(mysqli_error());
// $con = mysqli_connect("localhost", "root", "", "itechxpert");



#################### OR ##############################


define ( 'DB_HOST', 'localhost' );
define ( 'DB_USER', 'gmax' );
define ( 'DB_PASSWORD', 'gmaxpass' );
define ( 'DB_NAME', 'gmaxdb' );

$conn = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD) or die('Could not connect to database server.');
mysqli_select_db($conn,DB_NAME) or die('Could not select database.');

// $conn = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD,DB_NAME) or die('Could not connect to database server.');
// $conn = mysqli_connect("localhost", "root", "", "itechxpert");

Creat Table Users

CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`username` varchar(100) NOT NULL,
`email` varchar(100) NOT NULL,
`password` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

User Registration :-

File name : register.php

<!DOCTYPE html>
<html>
<head>
<title>User Registration in PHP with MySQL</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="header">
<h2>User Register</h2>
</div>

<form method="post" action="signup.php">
<?php include('errors.php'); ?>
<div class="input-group">
<label>Username</label>
<input type="text" name="username" value="<?php echo $username; ?>">
</div>
<div class="input-group">
<label>Email</label>
<input type="email" name="email" value="<?php echo $email; ?>">
</div>
<div class="input-group">
<label>Password</label>
<input type="password" name="password">
</div>
<div class="input-group">
<label>Confirm password</label>
<input type="password" name="confirm-password">
</div>
<div class="input-group">
<button type="submit" class="btn" name="submit">Register</button>
</div>
<p>
Already a member? <a href="login.php">Sign in</a>
</p>
</form>
</body>
</html>

User Registeration Action

when a user fillup the registration form and click on register button then call action form signup.php

signup page collect data from user registration form and store in the users table in database.

File name : signup.php

<?php
session_start();
include 'config.php';
include 'dbconnection.php';

// REGISTER USER
if (isset($_POST['submit'])) {
// first receive all input values from the form submit

$username = mysqli_real_escape_string($con, $_POST['username']);
$email = mysqli_real_escape_string($con, $_POST['email']);
$password = mysqli_real_escape_string($con, $_POST['password']);
$confirmpassword = mysqli_real_escape_string($con, $_POST['confirm-password']);

// form validation
// by adding (array_push()) corresponding error unto $errors array

if (empty($username)) { array_push($errors, "Username is required"); }
if (empty($email)) { array_push($errors, "Email is required"); }
if (empty($password)) { array_push($errors, "Password is required"); }
if ($password != $confirmpassword) {
array_push($errors, "password and Confirm passwords do not match");
}

// first check user does not already exist with the same username and/or email

$user_check = "SELECT * FROM users WHERE username='$username' OR email='$email' LIMIT 1";
$result = mysqli_query($con, $user_check);
$user = mysqli_fetch_assoc($result);

if ($user) {
// if user exists
if ($user['username'] === $username) {
array_push($errors, "Username already exists");
}

if ($user['email'] === $email) {
array_push($errors, "email already exists");
}
}

if (count($errors) == 0) {
$password = md5($password); //encrypt the password before saving in the database table

$qry = "INSERT INTO users (username, email, password) VALUES('$username', '$email', '$password')";
mysqli_query($con, $qry);
$_SESSION['username'] = $username;
$_SESSION['success'] = "You are now logged in";
header('location: index.php');
}
}

When a user is registered in the database, they are immediately logged in and redirected to the index.php page.

Error page :-

File name : errors.php

<?php
if (count($errors) > 0) :
?>
<div class="error">
<?php foreach ($errors as $error) : ?>
<p><?php echo $error ?></p>
<?php endforeach ?>
</div>
<?php
endif
?>

User Login Application

File name : login.php

<!DOCTYPE html>
<html>
<head>
<title>User Login in PHP with MySQL</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="header">
<h2>User Login Application</h2>
</div>

<form name="frm" method="post" action="login-action.php">
<?php include('errors.php'); ?>
<div class="input-group">
<label>Username</label>
<input type="text" name="username" >
</div>
<div class="input-group">
<label>Password</label>
<input type="password" name="password">
</div>
<div class="input-group">
<button type="submit" class="btn" name="submit">Login</button>
</div>
<p>
Not yet a member? <a href="register.php">Sign up</a>
</p>
</form>
</body>
</html>

Login Action

File name : login-action.php

<?php
session_start();
include 'config.php';
include 'dbconnection.php';

if (isset($_POST['submit'])) {
$username = mysqli_real_escape_string($con, $_POST['username']);
$password = mysqli_real_escape_string($con, $_POST['password']);

if (empty($username)) {
array_push($errors, "Username is required");
}
if (empty($password)) {
array_push($errors, "Password is required");
}

if (count($errors) == 0) {
$password = md5($password);
$qry = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$results = mysqli_query($con, $qry);
if (mysqli_num_rows($results) == 1) {
$_SESSION['username'] = $username;
$_SESSION['success'] = "You are now logged in";
header('location: home.php');
}else {
array_push($errors, "Wrong username/password combination");
}
}
}

?>

File name : home.php

<?php
session_start();

if (!isset($_SESSION['username'])) {
$_SESSION['msg'] = "You must log in first";
header('location: login.php');
}
if (isset($_GET['logout'])) {
session_destroy();
unset($_SESSION['username']);
header("location: login.php");
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>

<div class="header">
<h2>Home Page</h2>
</div>
<div class="content">
<!-- notification message -->
<?php if (isset($_SESSION['success'])) : ?>
<div class="error success" >
<h3>
<?php
echo $_SESSION['success'];
unset($_SESSION['success']);
?>
</h3>
</div>
<?php endif ?>

<!-- logged in user information -->
<?php if (isset($_SESSION['username'])) : ?>
<p>Welcome <strong><?php echo $_SESSION['username']; ?></strong></p>
<p> <a href="index.php?logout='1'" style="color: red;">logout</a> </p>
<?php endif ?>
</div>

</body>
</html>





Previous Next


Trending Tutorials




Review & Rating

0.0 / 5

0 Review

5
(0)

4
(0)

3
(0)

2
(0)

1
(0)

Write Review Here