Commit 7ea7b049 authored by Yankova, Liliya (UG - Computer Science)'s avatar Yankova, Liliya (UG - Computer Science)
Browse files

Register/Login/Logout with docker

parents
version: '3'
services:
db:
image: mysql:8.0
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_ROOT_PASSWORD: advancedroot
MYSQL_DATABASE: userdb
MYSQL_USER: service_user
MYSQL_PASSWORD: service_user
ports:
- "9906:3306"
web:
image: php:7.2.2-apache
container_name: php_web
depends_on:
- db
volumes:
- ./php/:/var/www/html/
ports:
- "8000:80"
stdin_open: true
tty: true
<?php
//Database credentials
define('DB_SERVER', 'db');
define('DB_PORT', 9906);
define('DB_USERNAME', 'service_user');
define('DB_PASSWORD', 'service_user');
define('DB_NAME', 'userdb');
//Connection to database
$link = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
//Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
?>
\ No newline at end of file
<!DOCTYPE html>
<html>
<head>
<title>Virtual kitchen</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<body>
<div class="container" id="container">
<div class="form-container sign-up-container">
<form action="register.php" method="POST">
<h1>Create Account</h1>
<input type="text" placeholder="Forename" name="forename" required/>
<input type="text" placeholder="Surname" name="surname" required/>
<input type="text" placeholder="Username" name="username" required/>
<input type="password" placeholder="Password" name="password" required/>
<input type="password" placeholder="Repeat Password" name="pass_repeat" required/>
<input type="email" placeholder="Email" name="email" required/>
<button type="submit">Sign Up</button>
</form>
</div>
<div class="form-container sign-in-container">
<form action="login.php" method="POST">
<h1>Sign in</h1>
<input type="text" placeholder="Username" name="username" required/>
<input type="password" placeholder="Password" name="password" required/>
<button type="submit">Sign In</button>
</form>
</div>
<div class="overlay-container">
<div class="overlay">
<div class="overlay-panel overlay-left">
<h1>Welcome Back!</h1>
<p>
To keep connected with us please login with your personal info
</p>
<button class="ghost" id="signIn">Sign In</button>
</div>
<div class="overlay-panel overlay-right">
<h1>Hello, Friend!</h1>
<p>Enter your personal details and start journey with us</p>
<button class="ghost" id="signUp">Sign Up</button>
</div>
</div>
</div>
</div>
<!-- This is JS -->
<script type="text/javascript" src="login_js.js"></script>
</body>
</html>
\ No newline at end of file
<?php
//Start session
session_start();
include("config.php");
//Variables to hold username and password from login form
$username = $_POST['username'];
$password = $_POST['password'];
if(!empty($_POST)){
//Checks if username and password are set (i.e. not empty)
if (isset($username) && isset($password)) {
$sql = $link->prepare("SELECT * FROM user WHERE username = ?");
$sql->bind_param('s', $username);
$sql->execute();
$result = $sql->get_result();
$user = $result->fetch_object();
//Checks the password against the one in the database
if(password_verify($password, $user->password) ) {
$_SESSION['ID'] = $user->ID;
header("Location: http://localhost:8000/page2.php");
} else{
echo '<script>
alert("Wrong password");
window.location.href = "index.html";
</script>';
}
}
}
?>
\ No newline at end of file
const signUpButton = document.getElementById('signUp');
const signInButton = document.getElementById('signIn');
const container = document.getElementById('container');
signUpButton.addEventListener('click', () => {
container.classList.add('right-panel-active');
});
signInButton.addEventListener('click', () => {
container.classList.remove('right-panel-active');
});
\ No newline at end of file
<?php
session_start();
unset($_SESSION["username"]);
unset($_SESSION["password"]);
header('Location: http://localhost:8000/index.html');
?>
\ No newline at end of file
<?php
session_start();
if (isset($_SESSION['ID'])) {
echo 'Session set';
?>
<!DOCTYPE html>
<html>
<head>
<title>Virtual kitchen</title>
<style type="text/css">
body{ font: 14px sans-serif; }
.wrapper{ width: 350px; padding: 20px; }
</style>
</head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<body>
<ul>
<li><a href="logout.php">Logout</a></li>
</ul>
</body>
<?php
} else {
echo '<script>
alert("Session not set");
window.location.href = "index.html";
</script>';
}
?>
</html>
\ No newline at end of file
<?php
include("config.php");
//Variables to hold all information from register form
$forename = $_POST['forename'];
$surname = $_POST['surname'];
$username = $_POST['username'];
$password1 = $_POST['password'];
$password2 = $_POST['pass_repeat'];
$hashed_password = password_hash($password1, PASSWORD_DEFAULT);
$email = $_POST['email'];
//Checks if the 2 passwords match
if($password1 != $password2){
echo "The passwords do not match. Please try again!";
exit;
}
$sql = "SELECT * FROM user WHERE username = '$username'";
$user_result = mysqli_query($link, $sql);
if($user_result->num_rows > 0){
echo "The username " . $username . " already exists in the database!";
exit;
}
//Adds the new user to the database with all relevant information
$sql = "INSERT INTO user (ID, username, forename, surname, password, email) VALUES (null, ?, ?, ?, ?, ?)";
$statement = mysqli_prepare($link, $sql);
$statement->bind_param("sssss", $username, $forename, $surname, $hashed_password, $email);
$result = $statement->execute();
//Checks if the add was successful and if the 2 passwords match
if($result && password_verify($password1, $hashed_password)){
echo '<script>
alert("You have successfully registered!");
window.location.href = "index.html";
</script>';
} else{
echo '<script>
alert("Failed to register.");
window.location.href = "index.html";
</script>';
}
?>
\ No newline at end of file
h1 {
font-weight: bold;
margin: 0;
}
p {
font-size: 14px;
font-weight: 100;
line-height: 20px;
letter-spacing: 0.5px;
margin: 20px 0 30px;
}
span {
font-size: 12px;
}
a {
color: #333;
font-size: 14px;
text-decoration: none;
margin: 15px 0;
}
button {
border-radius: 20px;
border: 1px solid #ff4b2b;
background-color: #ff4b2b;
color: #ffffff;
font-size: 12px;
font-weight: bold;
padding: 12px 45px;
letter-spacing: 1px;
text-transform: uppercase;
transition: transform 80ms ease-in;
}
button:active {
transform: scale(0.95);
}
button:focus {
outline: none;
}
button.ghost {
background-color: transparent;
border-color: #ffffff;
}
form {
background-color: #ffffff;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
padding: 0 50px;
height: 100%;
text-align: center;
}
input {
background-color: #eee;
border: none;
padding: 12px 15px;
margin: 8px 0;
width: 100%;
}
.social-container {
margin: 20px 0;
}
.social-container a {
border: 1px solid #dddddd;
border-radius: 50%;
display: inline-flex;
justify-content: center;
align-items: center;
margin: 0 5px;
height: 40px;
width: 40px;
}
.container {
background-color: #ffffff;
border-radius: 10px;
box-shadow: 0 14px 28px rgba(0, 0, 0, 0.25), 0 10px 10px rgba(0, 0, 0, 0.22);
position: relative;
overflow: hidden;
width: 1068px;
max-width: 100%;
min-height: 480px;
margin: auto;
}
.form-container {
position: absolute;
top: 0;
height: 100%;
transition: all 0.6s ease-in-out;
}
.sign-in-container {
left: 0;
width: 50%;
z-index: 2;
}
.sign-up-container {
left: 0;
width: 50%;
opacity: 0;
z-index: 1;
}
.container.right-panel-active .sign-in-container {
transform: translateX(100%);
}
.container.right-panel-active .sign-up-container {
transform: translateX(100%);
opacity: 1;
z-index: 5;
animation: show 0.6s;
}
@keyframes show {
0%,
49.99% {
opacity: 0;
z-index: 1;
}
50%,
100% {
opacity: 1;
z-index: 5;
}
}
.overlay-container {
position: absolute;
top: 0;
left: 50%;
width: 50%;
height: 100%;
overflow: hidden;
transition: transform 0.6s ease-in-out;
z-index: 100;
}
.container.right-panel-active .overlay-container {
transform: translateX(-100%);
}
.overlay {
background: #ff416c;
background: -webkit-linear-gradient(to right, #ff4b2b, #ff416c);
background: linear-gradient(to right, #ff4b2b, #ff416c);
background-repeat: no-repeat;
background-size: cover;
background-position: 0 0;
color: #ffffff;
position: relative;
left: -100%;
height: 100%;
width: 200%;
transform: translateX(0);
transition: transform 0.6s ease-in-out;
}
.container.right-panel-active .overlay {
transform: translateX(50%);
}
.overlay-panel {
position: absolute;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
padding: 0 40px;
text-align: center;
top: 0;
height: 100%;
width: 50%;
transform: translateX(0);
transition: transform 0.6s ease-in-out;
}
.overlay-left {
transform: translateX(-20%);
}
.container.right-panel-active .overlay-left {
transform: translateX(0);
}
.overlay-right {
right: 0;
transform: translateX(0);
}
.container.right-panel-active .overlay-right {
transform: translateX(20%);
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment