In: Computer Science
PHP Question:
Subject: Managing Persistence.
Objective:
• Create a login script.
• Strengthen passwords.
• Authenticate users.
• Create sessions.
• Create cookies.
• Manage persistence.
Description:
All dynamic sites need a way to manage access to resources. Many dynamic sites use back-end databases that can contain sensitive information that needs to be securely managed. At the same time, sites want to be inviting to guests. Managing the access to sensitive information is a critical function.
Requirements:
Update site for member access management.
Add login buttons to all pages of the site for users that are not authenticated (not logged in). Add logout buttons to every page of the site for members that have been authenticated (logged in).
• Login button should direct the user to a login form.
• The password should have a pattern policy implementation.
• Passwords should be saved encrypted to the database.
• Logout button should direct the member to a logout script.
• Add a welcome message to all pages for guests and members.
Create a new page called login.php.
Add form fields to collect the following information from users wanting to register on the site.
• Username
• Password
Process the form submission.
• Check for empty fields.
• Check for valid email address.
• Sanitize (trim) the data.
• Check the data against the database for a match.
• Set session variables and cookies upon user authentication.
• Redirect the authenticated user to a member's landing page with a
welcome message.
Note :1) In case any query please comment it , will revert back to you within 12 hours
Dont forget change the database username ,password and database name .
Also attached database description and databse password encrypted .
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Index Page</title>
</head>
<body>
<a href="login.php" > Login </a>
<a href="register.php" > Register </a>
</body>
</html>
connection.php
<?php
$host = "localhost";
$user = "root";
$password = 'root';
$db_name = "demo";
$con = mysqli_connect($host, $user, $password, $db_name);
if(mysqli_connect_errno()) {
die("Failed to connect with MySQL: ". mysqli_connect_error());
}
?>
login.php
<html>
<head>
<title>PHP login system</title>
<link rel = "stylesheet" type = "text/css" href = "style.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
<div id = "frm">
<form name="f1" action = "authentication.php" onsubmit = "return validation()" method = "POST" role="form">
<legend>Login Form </legend>
<div class="form-group" style="width: 250px;" >
<label for="">Username</label>
<input type="text" name="user" class="form-control" id="" placeholder="Input field">
</div>
<div class="form-group" style="width: 250px;" >
<label for="">Password</label>
<input type="password" name="pass" class="form-control" id="" placeholder="Input field">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
<script>
function validation()
{
var id=document.f1.user.value;
var ps=document.f1.pass.value;
if(id.length=="" && ps.length=="") {
alert("User Name and Password fields are empty");
return false;
}
else
{
if(id.length=="") {
alert("User Name is empty");
return false;
}
if (ps.length=="") {
alert("Password field is empty");
return false;
}
}
}
</script>
</body>
</html>
authentication.php
<?php
include('connection.php');
session_start();
$username = $_POST['user'];
$password = md5($_POST['pass']);
//to prevent from mysqli injection
$username = stripcslashes($username);
$password = stripcslashes($password);
$username = mysqli_real_escape_string($con, $username);
$password = mysqli_real_escape_string($con, $password);
$sql = "select * from students where username = '$username' and password = '$password'";
$result = mysqli_query($con, $sql);
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
$count = mysqli_num_rows($result);
if($count == 1){
$_SESSION['name'] = $_POST['user'];
header("location: welcome.php");
}
else{
echo "<h1> Login failed. Invalid username or password.</h1>";
}
?>
register.php
<?php include('server.php') ?>
<!DOCTYPE html>
<html>
<head>
<title>Registration</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
<div class="header">
</div>
<form method="post" action="register.php">
<div class="input-group">
<label>Username</label>
<input type="text" name="username" >
</div>
<div class="input-group">
<label>Email</label>
<input type="email" name="email" >
</div>
<div class="input-group">
<label>Password</label>
<input type="password" name="password_1">
</div>
<div class="input-group">
<label>Confirm password</label>
<input type="password" name="password_2">
</div>
<div class="input-group">
<button type="submit" class="btn" name="reg_user">Register</button>
</div>
<p>
Already a member? <a href="login.php">Sign in</a>
</p>
</form>
</body>
</html>
server.php
<?php
session_start();
$db = mysqli_connect('localhost', 'root', 'root', 'demo');
if (isset($_POST['reg_user'])) {
$username = mysqli_real_escape_string($db, $_POST['username']);
$email = mysqli_real_escape_string($db, $_POST['email']);
$password_1 = mysqli_real_escape_string($db, $_POST['password_1']);
$password_2 = mysqli_real_escape_string($db, $_POST['password_2']);
if (empty($username)) { array_push($errors, "Username is required"); }
if (empty($email)) { array_push($errors, "Email is required"); }
if (empty($password_1)) { array_push($errors, "Password is required"); }
if ($password_1 != $password_2) {
array_push($errors, "The two passwords do not match");
}
$user_check_query = "SELECT * FROM students WHERE username='$username' OR email='$email' LIMIT 1";
$result = mysqli_query($db, $user_check_query);
$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_1);//encrypt the password before saving in the database
$query = "INSERT INTO students (email,username, password)
VALUES('$email','$username', '$password')";
mysqli_query($db, $query);
$_SESSION['username'] = $username;
$_SESSION['success'] = "You are now logged in";
header('location: index.html');
}
}
welcome.php
<?php
include('connection.php');
session_start();
?>
<html>
<head>
<title>Welcome </title>
</head>
<body>
<h1>Welcome <?php echo $_SESSION['name']; ?></h1>
<h2><a href = "logout.php">Sign Out</a></h2>
</body>
</html>
logout.php
<?php
session_destroy();
header("location: index.html");
?>
output: