Question

In: Computer Science

Having those files please adjust the following tasks. When the user fills out the registration, all...

Having those files please adjust the following tasks.

When the user fills out the registration, all information is inserted into a database table.

When the user signs in, all information is verified from the database table.

You can either create your own table on your machine or use the demo server table for testing. Remember the table name and the fields must be exactly as shown below.

If you are using your own table, you must change the connection info as shown below before uploading to the demo server. Then you must test to make sure it works correctly on the demo server.

The table name will be 'account'.

The fields in the table are:

username - 20 char

password - 50 char

name - 20 char

email - 50 char

Make sure that all field names are the same upper and lowercase letters as shown above.

The password will still be encrypted when stored in the database.

You must insert data into the table using your program. Do not assume anything is on the table.

When creating userids and passwords, use something unique, as your classmates will be sharing the same table.

assignment3

<?php
session_start();   
if(isset($_SESSION['userid']))   
{
header("Location:home.php");   
}
include 'header.php';
?>

<?php

if(isset($_POST["register"]))   
{
// collect form data

$first_name=$_POST["first_name"];
$last_name=$_POST["last_name"];
$email=$_POST["email"];
$userid=$_POST["userid"];
$password=$_POST["password"];
$verify_password=$_POST["verify_password"];

if($password===$verify_password) // password verification
{

$hashFormat="$2y$10$"; // set hash
$salt ="iusesomecrazystrings22"; // set salt
$new_salt = $hashFormat . $salt; // concatenate hash and salt
$encrypted_password = crypt($password,$new_salt); // Encrypt the password

if(file_exists("users.txt")) // check file is exist or not
{
// Open a file for write only and append the data to an existing file

$myfile = fopen("users.txt", "a");

$mydata=$first_name."|".$last_name."|".$email."|".$userid."|".$encrypted_password."\r\n";

fwrite($myfile, $mydata);

include('send_mail.php');


}


else
{
$myfile = fopen("users.txt", "w"); // Open a file for write only

fwrite($myfile, "First Name |Last Name | Email| User Id | Password\r\n");

$mydata=$first_name."|".$last_name."|".$email."|".$userid."|".$encrypted_password."\r\n";

fwrite($myfile, $mydata);

include ("send_mail.php");

}


fclose($myfile);   

// set the message if password is verified

$register_msg = "The following registration information has been successfully submitted";
}
else
{
$register_msg = "password not match"; // set the message if password is not verified
}
}
else
{
$register_msg = "";   
}

?>

<html>

<body>   

<h1> <?php echo $register_msg; ?> </h1>   

<form action="assignment3.php" method="POST" style="float:left; width:50%;">
<fieldset>
<legend style="text-align: center;">User Information</legend>
<table style="margin:0px auto;">   
<tr>
<td>
<label> First Name</label>
</td>
<td>
<input type="text" name="first_name">   
</td>
</tr>
<tr>
<td>
<label> Last Name</label>   
</td>
<td>
<input type="text" name="last_name">
</td>
</tr>
<tr>
<td>
<label> Email</label>
</td>
<td>
<input type="email" name="email">   
</td>
</tr>
<tr>
<td>
<label> User ID</label>
</td>
<td>
<input type="text" name="userid">   
</td>
</tr>
<tr>
<td>
<label> Password</label>
</td>
<td>

<input type="password" name="password" pattern="(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,12}" title="Must contain at least one number and one special character, and length must between 8 to 12 characters">
</td>
</tr>
<tr>
<td>
<label> Verify Password</label>
</td>
<td>
<!--create a password field for Verify Password -->

<input type="password" name="verify_password" pattern="(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,12}" title="Must contain at least one number and one special character, and length must between 8 to 12 characters">
</td>
</tr>

<tr>
<td><input type="submit" name="register" value="Register"></td>
<td><input type="reset" name="reset" value="Reset"></td>
</tr>
</table>
</fieldset>
</form>   

<form action="home.php" method="POST" style="float:right; width:50%;">
<fieldset>   
<legend style="text-align: center;">Sign In</legend>
<table style="margin:0px auto;">   
<tr>
<td>
<label> User ID</label>
</td>
<td>
<input type="text" name="userid">   
</td>
</tr>
<tr>
<td>
<label> Password</label>
</td>
<td>

<input type="password" name="password" pattern="(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,12}" title="Must contain at least one number and one special character, and length must between 8 to 12 characters">
</td>
</tr>
<tr>
<td><input type="submit" name="Submit" value="Submit"></td>
<td><input type="reset" name="reset" value="Reset"></td>
</tr>
</table>   
</fieldset>   
</form>   

</body>
</html>

header.php

<!DOCTYPE html>
<html>
<head>
<title>Assignment3</title>
<link rel="stylesheet" type="text/css" href="main.css" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<header>
<h1>Register for an Account</h1>
</header>

home.php

<?php

session_start();   

if(!isset($_POST["Submit"])&&!isset($_SESSION["userid"]))
{
header("Location:assignment3.php");   
}

if(isset($_POST['logout']))
{
  
session_unset();

// destroy the session
session_destroy();

header("Location:assignment2.php");
}

if(isset($_POST["Submit"])) // if Submit parameter exist
{
// collect form data

$userid=$_POST["userid"];
$password=$_POST["password"];

$hashFormat="$2y$10$"; // set hash
$salt ="iusesomecrazystrings22"; // set salt
$new_salt = $hashFormat . $salt; // concatenate hash and salt

$encrypted_password = crypt($password,$new_salt); // Encrypt the password entered by the user

$myfile = file ('./users.txt');

foreach($myfile as $row)
{   
$data = explode("|",$row);

// Compare the userid and password to what is in the text file

if($userid===$data[3] && $encrypted_password===trim($data[4]))
{
// Set session variables

$_SESSION['userid'] = $data[3]; // Set userid to session variable
$_SESSION['password'] = $data[4]; // Set password to session variable

// If password match, set display a welcome message to variable

$msg="Welcome ".$_SESSION['userid'];
}
else
{
// If password do not match, set display this message to variable

$msg="userid/password combo incorrect please reenter";
}
}
}
else
{
$msg="";
}
  
?>

<html>   
<body>
<h1> <?php echo $msg; ?> </h1>

<?php
if(isset($_SESSION["userid"]))   
{
?>

<form action="home.php" method="post">
<input type="submit" value="Sign Out" name="logout">
</form>

<?php
}
?>

</body>
</html>

css file

html {
background-color: #e6f2ff;
}
body {
font-family: Arial, Helvetica, sans-serif;
width: 900px;
margin: 0 auto;
padding: 0 1em;
background-color: white;
border: 1px solid blue;
}
header {
border-bottom: 2px solid black;
padding: .5em 0;
}
header h1 {
color: blue;
}
main {

}
aside {
float: left;
width: 150px;
}


h1 {
font-size: 150%;
margin: 0;
padding: .5em 0 .25em;
}
h2 {
font-size: 120%;
margin: 0;
padding: .75em 0 0;
}
h1, h2 {
color: black;
}

fieldset {
margin: 1em;
padding-top: 1em;
margin-left: 10px;
border: 1px solid blue;
  
}

label {
float: left;
width: 10em;
text-align: right;
margin-top: .25em;
margin-bottom: .5em;
}

input, select {
margin-left: 0.5em;
margin-bottom: 0.5em;
width: 14em;
}

br {
clear: both;
}
span {
vertical-align: middle;
}

.error {
color: #cc3300;
}

.notice {
color: #cc3300;
font-size: 50%;
text-align: right;
}

Solutions

Expert Solution

assignment3.php

<?php
session_start();
if(isset($_SESSION['userid']))
{
header("Location:home.php");
}
include 'header.php';
include 'dbconn.php';
?>

<?php

if(isset($_POST["register"]))
{
// collect form data

$first_name=$_POST["first_name"];
$last_name=$_POST["last_name"];
$email=$_POST["email"];
$userid=$_POST["userid"];
$password=$_POST["password"];
$verify_password=$_POST["verify_password"];

// Inserting data from text boxes to the table account
$sql ="INSERT INTO account(username,password,name,email) VALUES ('$userid','$password','$first_name','$email')";

   if ($conn->query($sql) === TRUE) {
echo "</br>"."New record created successfully"."</br>"; //the statements printed are for testing
} else {
echo "</br>"."Error: " . $sql . "<br>" . $conn->error;
}
  

if($password===$verify_password) // password verification
{

$hashFormat="$2y$10$"; // set hash
$salt ="iusesomecrazystrings22"; // set salt
$new_salt = $hashFormat . $salt; // concatenate hash and salt
$encrypted_password = crypt($password,$new_salt); // Encrypt the password

if(file_exists("users.txt")) // check file is exist or not
{
// Open a file for write only and append the data to an existing file

$myfile = fopen("users.txt", "a");

$mydata=$first_name."|".$last_name."|".$email."|".$userid."|".$encrypted_password."\r\n";

fwrite($myfile, $mydata);
include ("send_mail.php");       //If this php doesnot exists, remove this line

}

else
{
$myfile = fopen("users.txt", "w"); // Open a file for write only

fwrite($myfile, "First Name |Last Name | Email| User Id | Password\r\n");

$mydata=$first_name."|".$last_name."|".$email."|".$userid."|".$encrypted_password."\r\n";

fwrite($myfile, $mydata);
include ("send_mail.php");                                 //If this php doesnot exists, remove this line

}

fclose($myfile);

// set the message if password is verified

$register_msg = "The following registration information has been successfully submitted";
}
else
{
$register_msg = "password not match"; // set the message if password is not verified
}
}
else
{
$register_msg = "";
}

?>

<html>

<body>

<h1> <?php echo $register_msg; ?> </h1>

<form action="assignment3.php" method="POST" style="float:left; width:50%;">
<fieldset>
<legend style="text-align: center;">User Information</legend>
<table style="margin:0px auto;">
<tr>
<td>
<label> First Name</label>
</td>
<td>
<input type="text" name="first_name">
</td>
</tr>
<tr>
<td>
<label> Last Name</label>
</td>
<td>
<input type="text" name="last_name">
</td>
</tr>
<tr>
<td>
<label> Email</label>
</td>
<td>
<input type="email" name="email">
</td>
</tr>
<tr>
<td>
<label> User ID</label>
</td>
<td>
<input type="text" name="userid">
</td>
</tr>
<tr>
<td>
<label> Password</label>
</td>
<td>

<input type="password" name="password" pattern="(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,12}" title="Must contain at least one number and one special character, and length must between 8 to 12 characters">
</td>
</tr>
<tr>
<td>
<label> Verify Password</label>
</td>
<td>
<!--create a password field for Verify Password -->

<input type="password" name="verify_password" pattern="(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,12}" title="Must contain at least one number and one special character, and length must between 8 to 12 characters">
</td>
</tr>

<tr>
<td><input type="submit" name="register" value="Register"></td>
<td><input type="reset" name="reset" value="Reset"></td>
</tr>
</table>
</fieldset>
</form>

<form action="home.php" method="POST" style="float:right; width:50%;">
<fieldset>
<legend style="text-align: center;">Sign In</legend>
<table style="margin:0px auto;">
<tr>
<td>
<label> User ID</label>
</td>
<td>
<input type="text" name="userid">
</td>
</tr>
<tr>
<td>
<label> Password</label>
</td>
<td>

<input type="password" name="password" pattern="(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,12}" title="Must contain at least one number and one special character, and length must between 8 to 12 characters">
</td>
</tr>
<tr>
<td><input type="submit" name="Submit" value="Submit"></td>
<td><input type="reset" name="reset" value="Reset"></td>
</tr>
</table>
</fieldset>
</form>

</body>
</html>

header.php

It works perfectly as it is.

home.php

<!DOCTYPE html>
<html lang="en">
<head

<link rel="stylesheet" type="text/css" href="style.css">   //style.css is the css file mentioned in this program and can also rename it
</head>

<?php

session_start();

if(!isset($_POST["Submit"])&&!isset($_SESSION["userid"]))
{
header("Location:Assignment3.php");
}

if(isset($_POST['logout']))
{

session_unset();

// destroy the session
session_destroy();

header("Location:assignment2.php");
}

if(isset($_POST["Submit"])) // if Submit parameter exist
{
// collect form data

$userid=$_POST["userid"];
$password=$_POST["password"];

$hashFormat="$2y$10$"; // set hash
$salt ="iusesomecrazystrings22"; // set salt
$new_salt = $hashFormat . $salt; // concatenate hash and salt

$encrypted_password = crypt($password,$new_salt); // Encrypt the password entered by the user

$myfile = file ('./users.txt');

foreach($myfile as $row)
{
$data = explode("|",$row);

// Compare the userid and password to what is in the text file

if($userid===$data[3] && $encrypted_password===trim($data[4]))
{
// Set session variables

$_SESSION['userid'] = $data[3]; // Set userid to session variable
$_SESSION['password'] = $data[4]; // Set password to session variable

// If password match, set display a welcome message to variable

$msg="Welcome ".$_SESSION['userid'];
}
else
{
// If password do not match, set display this message to variable

$msg="userid/password combo incorrect please reenter";
}
}
}
else
{
$msg="";
}

?>

<html>
<body>
<h1> <?php echo $msg; ?> </h1>

<?php
if(isset($_SESSION["userid"]))
{
?>

<form action="home.php" method="post">
<input type="submit" value="Sign Out" name="logout">
</form>

<?php
}
?>

</body>
</html>

main.css

It works perfectly as it is.

dbconn.php

<!DOCTYPE html>
<html>
<body>

<?php
$servername = "localhost";
$username = "Uname";          //username and password can be given according to you
$password = "password";
$dbname="accounting";       //dbname=database name, can be renamed


// Create connection
$conn = new mysqli($servername,$username,$password,$dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully"."</br>";

//Creating Database
$sql = "CREATE DATABASE accounting";
if ($conn->query($sql) === TRUE) {
echo "Database created successfully"."</br>";
} else {
echo "Error creating database: " ."</br>". $conn->error;
}
//Create Table account
$sql="CREATE TABLE account(username VARCHAR(20),password varchar(50),name varchar(20),email varchar(50))";
if ($conn->query($sql) === TRUE) {
echo "</br>"."Table account created successfully"."</br>";
} else {
echo "</br>"."Error creating table: "."</br>" . $conn->error;
}

?>
</body>
</html>

Sample output:

The statements below "Register For account" heading are for testing the database operations, they can be removed from dbconn.php file.

Cant create database means that already database is existing.

Error creating table means that the table "account" is already created.

Test Cases:

1.

2.

MH was the user name given for testing.


Related Solutions

A successful new user registration requires the following valid data: ⦁   User name: it must start...
A successful new user registration requires the following valid data: ⦁   User name: it must start with a letter and consist of only letters and digits without any space character. User name is case insensitive. Different users must have different user names. ⦁   Password: it must have at least six characters, consist of only letters, digits, and special characters (@, #, $, ^, &), and at least one upper-case letter, one lower-case letter, one digit, and one special character. ⦁  ...
In Python: Please complete the following two tasks in Tic Tac Toe: 1. Allow the user...
In Python: Please complete the following two tasks in Tic Tac Toe: 1. Allow the user to choose heads/tails to see if the user goes first or the computer and alter your code accordingly. 2. Allow the user to play again. Add a simple strategy for the AI to play against the user.
Transient heat transfer Leaking steam completely fills a room, pushing out all of the air. The...
Transient heat transfer Leaking steam completely fills a room, pushing out all of the air. The steam is at atmospheric pressure. Because the mortar cement siding walls are initially cool, at a uniform temperature of 22C, the steam begins to condense on the walls. Find the rate of condensation per square meter of wall at (a) 10 s and (b) 30 s after the leak. The wall is 10 cm thick, and you may assume that the other side of...
I am having trouble with the entry to adjust accumulated depreciation, can you please explain.
I am having trouble with the entry to adjust accumulated depreciation, can you please explain.
DONE IN POWERSHELL create a function that allows the user to perform the following tasks a....
DONE IN POWERSHELL create a function that allows the user to perform the following tasks a. Create a folder named NetworkInformation Create a file in the above folder named NetworkInformation.txt. Pipe the Get-NetTCPConnection, Get-NetNeighbor, and Get-NetUDPEndPoint cmdlets output into the file
Assume user will enter letters or numbers that are out of range. When user inputs invalid...
Assume user will enter letters or numbers that are out of range. When user inputs invalid values, show an alert message and ask user to enter a valid value again. Validate first and then proceed with the program. isNaN() method may be useful. Must validate user input. 1. Suggested Filename: fortune.html & fortune.js Write a program that simulates a fortune cookie. The program should display one of five unique fortunes, depending on user input. The user must enter a number...
please show all work: A machine that fills beverage cans is supposed to put 12 ounces...
please show all work: A machine that fills beverage cans is supposed to put 12 ounces of beverage in each can. The standard deviation of the amount in each can is 0.12 ounce. The machine is overhauled with new components, and ten cans are filled to determine whether the standard deviation has changed. Assume the fill amounts to be a random sample from a normal population. 12.14, 12.05, 12.27, 11.89, 12.06, 12.14, 12.05, 12.38, 11.92, 12.14 Perform a hypothesis test...
Please perform the following tasks using a Timer Control: When an NO Push Button (I:0/0) is...
Please perform the following tasks using a Timer Control: When an NO Push Button (I:0/0) is pressed (I0) and released, a Pilot Light (O:0/0) will be on and after 7 seconds (T4:0), a single-active cylinder will be pushed forward. The piston will remain at its position for another 5 seconds (T4:1) and then it will automatically retract. The pilot light will be off and the timer will also be automatically reset. The whole cycle can then be repeated.
Write an array method to carry out each of the following tasks for an array of...
Write an array method to carry out each of the following tasks for an array of integers. Note: you can think of each as a separate problem independent of the others. a) Swap the first and last elements in the array. b) Shi< all elements by one to the right and move the last element into the first posi>on. For example, 1 4 9 16 25 would be transformed into 25 1 4 9 16. c) Replace all even elements...
Write a program that asks the user for an integer and then prints out all its...
Write a program that asks the user for an integer and then prints out all its factors. For example, when the user enters 84, the program should print 2 2 3 7 Validate the input to make sure that it is not a character or a string using do loop.in c++
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT