Skip to content

Commit c829b80

Browse files
authored
Add files via upload
1 parent 07b391f commit c829b80

File tree

10 files changed

+850
-0
lines changed

10 files changed

+850
-0
lines changed

assets/screenshot.png

6.96 KB
Loading

authenticate.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
// Start the session
3+
session_start();
4+
// Change the below variables to reflect your MySQL database details
5+
$DATABASE_HOST = 'localhost';
6+
$DATABASE_USER = 'root';
7+
$DATABASE_PASS = '';
8+
$DATABASE_NAME = 'phplogin';
9+
// Try and connect using the info above
10+
$con = mysqli_connect($DATABASE_HOST, $DATABASE_USER, $DATABASE_PASS, $DATABASE_NAME);
11+
// Check for connection errors
12+
if (mysqli_connect_errno()) {
13+
// If there is an error with the connection, stop the script and display the error
14+
exit('Failed to connect to MySQL: ' . mysqli_connect_error());
15+
}
16+
// Now we check if the data from the login form was submitted, isset() will check if the data exists
17+
if (!isset($_POST['username'], $_POST['password'])) {
18+
// Could not get the data that should have been sent
19+
exit('Please fill both the username and password fields!');
20+
}
21+
// Prepare our SQL, which will prevent SQL injection
22+
if ($stmt = $con->prepare('SELECT id, password FROM accounts WHERE username = ?')) {
23+
// Bind parameters (s = string, i = int, b = blob, etc), in our case the username is a string so we use "s"
24+
$stmt->bind_param('s', $_POST['username']);
25+
$stmt->execute();
26+
// Store the result so we can check if the account exists in the database
27+
$stmt->store_result();
28+
// Check if account exists with the input username
29+
if ($stmt->num_rows > 0) {
30+
// Account exists, so bind the results to variables
31+
$stmt->bind_result($id, $password);
32+
$stmt->fetch();
33+
// Note: remember to use password_hash in your registration file to store the hashed passwords
34+
if (password_verify($_POST['password'], $password)) {
35+
// Password is correct! User has logged in!
36+
// Regenerate the session ID to prevent session fixation attacks
37+
session_regenerate_id();
38+
// Declare session variables (they basically act like cookies but the data is remembered on the server)
39+
$_SESSION['account_loggedin'] = TRUE;
40+
$_SESSION['account_name'] = $_POST['username'];
41+
$_SESSION['account_id'] = $id;
42+
// Redirect to the home page
43+
header('Location: home.php');
44+
exit;
45+
} else {
46+
// Incorrect password
47+
echo 'Incorrect username and/or password!';
48+
}
49+
} else {
50+
// Incorrect username
51+
echo 'Incorrect username and/or password!';
52+
}
53+
// Close the prepared statement
54+
$stmt->close();
55+
}
56+
?>

home.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
// We need to use sessions, so you should always initialize sessions using the below function
3+
session_start();
4+
// If the user is not logged in, redirect to the login page
5+
if (!isset($_SESSION['account_loggedin'])) {
6+
header('Location: index.php');
7+
exit;
8+
}
9+
?>
10+
<!DOCTYPE html>
11+
<html>
12+
<head>
13+
<meta charset="utf-8">
14+
<meta name="viewport" content="width=device-width,minimum-scale=1">
15+
<title>Home</title>
16+
<link href="style.css" rel="stylesheet" type="text/css">
17+
</head>
18+
<body>
19+
20+
<header class="header">
21+
22+
<div class="wrapper">
23+
24+
<h1>Website Title</h1>
25+
26+
<nav class="menu">
27+
<a href="home.php">Home</a>
28+
<a href="profile.php">Profile</a>
29+
<a href="logout.php">
30+
<svg width="12" height="12" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--!Font Awesome Free 6.5.1 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2024 Fonticons, Inc.--><path d="M377.9 105.9L500.7 228.7c7.2 7.2 11.3 17.1 11.3 27.3s-4.1 20.1-11.3 27.3L377.9 406.1c-6.4 6.4-15 9.9-24 9.9c-18.7 0-33.9-15.2-33.9-33.9l0-62.1-128 0c-17.7 0-32-14.3-32-32l0-64c0-17.7 14.3-32 32-32l128 0 0-62.1c0-18.7 15.2-33.9 33.9-33.9c9 0 17.6 3.6 24 9.9zM160 96L96 96c-17.7 0-32 14.3-32 32l0 256c0 17.7 14.3 32 32 32l64 0c17.7 0 32 14.3 32 32s-14.3 32-32 32l-64 0c-53 0-96-43-96-96L0 128C0 75 43 32 96 32l64 0c17.7 0 32 14.3 32 32s-14.3 32-32 32z"/></svg>
31+
Logout
32+
</a>
33+
</nav>
34+
35+
</div>
36+
37+
</header>
38+
39+
<div class="content">
40+
41+
<div class="page-title">
42+
<div class="wrap">
43+
<h2>Home</h2>
44+
<p>Welcome back, <?=htmlspecialchars($_SESSION['account_name'], ENT_QUOTES)?>!</p>
45+
</div>
46+
</div>
47+
48+
<div class="block">
49+
50+
<p>This is the home page. You are logged in!</p>
51+
52+
</div>
53+
54+
</div>
55+
56+
</body>
57+
</html>

index.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
// We need to use sessions, so you should always initialize sessions using the below function
3+
session_start();
4+
// If the user is logged in, redirect to the home page
5+
if (isset($_SESSION['account_loggedin'])) {
6+
header('Location: home.php');
7+
exit;
8+
}
9+
?>
10+
<!DOCTYPE html>
11+
<html>
12+
<head>
13+
<meta charset="utf-8">
14+
<meta name="viewport" content="width=device-width,minimum-scale=1">
15+
<title>Login</title>
16+
<link href="style.css" rel="stylesheet" type="text/css">
17+
</head>
18+
<body>
19+
<div class="login">
20+
21+
<h1>Member Login</h1>
22+
23+
<form action="authenticate.php" method="post" class="form login-form">
24+
25+
<label class="form-label" for="username">Username</label>
26+
<div class="form-group">
27+
<svg class="form-icon-left" width="14" height="14" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><!--!Font Awesome Free 6.5.1 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2024 Fonticons, Inc.--><path d="M224 256A128 128 0 1 0 224 0a128 128 0 1 0 0 256zm-45.7 48C79.8 304 0 383.8 0 482.3C0 498.7 13.3 512 29.7 512H418.3c16.4 0 29.7-13.3 29.7-29.7C448 383.8 368.2 304 269.7 304H178.3z"/></svg>
28+
<input class="form-input" type="text" name="username" placeholder="Username" id="username" required>
29+
</div>
30+
31+
<label class="form-label" for="password">Password</label>
32+
<div class="form-group mar-bot-5">
33+
<svg class="form-icon-left" xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 448 512"><!--!Font Awesome Free 6.5.1 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2024 Fonticons, Inc.--><path d="M144 144v48H304V144c0-44.2-35.8-80-80-80s-80 35.8-80 80zM80 192V144C80 64.5 144.5 0 224 0s144 64.5 144 144v48h16c35.3 0 64 28.7 64 64V448c0 35.3-28.7 64-64 64H64c-35.3 0-64-28.7-64-64V256c0-35.3 28.7-64 64-64H80z"/></svg>
34+
<input class="form-input" type="password" name="password" placeholder="Password" id="password" required>
35+
</div>
36+
37+
<button class="btn blue" type="submit">Login</button>
38+
39+
<p class="register-link">Don't have an account? <a href="register.php" class="form-link">Register</a></p>
40+
41+
</form>
42+
43+
</div>
44+
</body>
45+
</html>

logout.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
// Start the session
3+
session_start();
4+
// Destroy the active session, which logs the user out
5+
session_destroy();
6+
// Redirect to the login pag
7+
header('Location: index.php');
8+
exit;
9+
?>

phplogin.sql

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
CREATE DATABASE IF NOT EXISTS `phplogin` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
2+
USE `phplogin`;
3+
4+
CREATE TABLE IF NOT EXISTS `accounts` (
5+
`id` int(11) NOT NULL AUTO_INCREMENT,
6+
`username` varchar(50) NOT NULL,
7+
`password` varchar(255) NOT NULL,
8+
`email` varchar(100) NOT NULL,
9+
`registered` datetime NOT NULL,
10+
PRIMARY KEY (`id`)
11+
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
12+
13+
INSERT INTO `accounts` (`id`, `username`, `password`, `email`, `registered`) VALUES (1, 'test', '$2y$10$SfhYIDtn.iOuCW7zfoFLuuZHX6lja4lF4XA4JqNmpiH/.P3zB8JCa', 'test@example.com', '2025-01-01 00:00:00');

profile.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
// We need to use sessions, so you should always initialize sessions using the below function
3+
session_start();
4+
// If the user is not logged in, redirect to the login page
5+
if (!isset($_SESSION['account_loggedin'])) {
6+
header('Location: index.php');
7+
exit;
8+
}
9+
// Change the below variables to reflect your MySQL database details
10+
$DATABASE_HOST = 'localhost';
11+
$DATABASE_USER = 'root';
12+
$DATABASE_PASS = '';
13+
$DATABASE_NAME = 'phplogin';
14+
// Try and connect using the info above
15+
$con = mysqli_connect($DATABASE_HOST, $DATABASE_USER, $DATABASE_PASS, $DATABASE_NAME);
16+
// Ensure there are no connection errors
17+
if (mysqli_connect_errno()) {
18+
exit('Failed to connect to MySQL: ' . mysqli_connect_error());
19+
}
20+
// We don't have the email or registered info stored in sessions so instead we can get the results from the database
21+
$stmt = $con->prepare('SELECT email, registered FROM accounts WHERE id = ?');
22+
// In this case, we can use the account ID to get the account info
23+
$stmt->bind_param('i', $_SESSION['account_id']);
24+
$stmt->execute();
25+
$stmt->bind_result($email, $registered);
26+
$stmt->fetch();
27+
$stmt->close();
28+
?>
29+
<!DOCTYPE html>
30+
<html>
31+
<head>
32+
<meta charset="utf-8">
33+
<meta name="viewport" content="width=device-width,minimum-scale=1">
34+
<title>Home</title>
35+
<link href="style.css" rel="stylesheet" type="text/css">
36+
</head>
37+
<body>
38+
39+
<header class="header">
40+
41+
<div class="wrapper">
42+
43+
<h1>Website Title</h1>
44+
45+
<nav class="menu">
46+
<a href="home.php">Home</a>
47+
<a href="profile.php">Profile</a>
48+
<a href="logout.php">
49+
<svg width="12" height="12" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--!Font Awesome Free 6.5.1 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2024 Fonticons, Inc.--><path d="M377.9 105.9L500.7 228.7c7.2 7.2 11.3 17.1 11.3 27.3s-4.1 20.1-11.3 27.3L377.9 406.1c-6.4 6.4-15 9.9-24 9.9c-18.7 0-33.9-15.2-33.9-33.9l0-62.1-128 0c-17.7 0-32-14.3-32-32l0-64c0-17.7 14.3-32 32-32l128 0 0-62.1c0-18.7 15.2-33.9 33.9-33.9c9 0 17.6 3.6 24 9.9zM160 96L96 96c-17.7 0-32 14.3-32 32l0 256c0 17.7 14.3 32 32 32l64 0c17.7 0 32 14.3 32 32s-14.3 32-32 32l-64 0c-53 0-96-43-96-96L0 128C0 75 43 32 96 32l64 0c17.7 0 32 14.3 32 32s-14.3 32-32 32z"/></svg>
50+
Logout
51+
</a>
52+
</nav>
53+
54+
</div>
55+
56+
</header>
57+
58+
<div class="content">
59+
60+
<div class="page-title">
61+
<div class="wrap">
62+
<h2>Profile</h2>
63+
<p>View your profile details below.</p>
64+
</div>
65+
</div>
66+
67+
<div class="block">
68+
69+
<div class="profile-detail">
70+
<strong>Username</strong>
71+
<?=htmlspecialchars($_SESSION['account_name'])?>
72+
</div>
73+
74+
<div class="profile-detail">
75+
<strong>Email</strong>
76+
<?=htmlspecialchars($email)?>
77+
</div>
78+
79+
<div class="profile-detail">
80+
<strong>Registered</strong>
81+
<?=htmlspecialchars($registered)?>
82+
</div>
83+
84+
</div>
85+
86+
</div>
87+
88+
</body>
89+
</html>

register-process.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
// Change the below variables to reflect your MySQL database details
3+
$DATABASE_HOST = 'localhost';
4+
$DATABASE_USER = 'root';
5+
$DATABASE_PASS = '';
6+
$DATABASE_NAME = 'phplogin';
7+
// Try and connect using the info above
8+
$con = mysqli_connect($DATABASE_HOST, $DATABASE_USER, $DATABASE_PASS, $DATABASE_NAME);
9+
// Check for connection errors
10+
if (mysqli_connect_errno()) {
11+
// If there is an error with the connection, stop the script and display the error
12+
exit('Failed to connect to MySQL: ' . mysqli_connect_error());
13+
}
14+
// We can utilize the isset() function to check if the form has been submitted
15+
if (!isset($_POST['username'], $_POST['password'], $_POST['email'])) {
16+
// Could not get the data that should have been sent
17+
exit('Please complete the registration form!');
18+
}
19+
// Make sure the submitted registration values are not empty
20+
if (empty($_POST['username']) || empty($_POST['password']) || empty($_POST['email'])) {
21+
// One or more values are empty.
22+
exit('Please complete the registration form');
23+
}
24+
// Validate email address
25+
if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
26+
exit('Email is not valid!');
27+
}
28+
// Validate username (must be alphanumeric)
29+
if (preg_match('/^[a-zA-Z0-9]+$/', $_POST['username']) == 0) {
30+
exit('Username is not valid!');
31+
}
32+
// Validate password (between 5 and 20 characters long)
33+
if (strlen($_POST['password']) > 20 || strlen($_POST['password']) < 5) {
34+
exit('Password must be between 5 and 20 characters long!');
35+
}
36+
// Check if the username already exists
37+
if ($stmt = $con->prepare('SELECT id, password FROM accounts WHERE username = ?')) {
38+
// Bind parameters (s = string, i = int, b = blob, etc)
39+
$stmt->bind_param('s', $_POST['username']);
40+
$stmt->execute();
41+
// Store the result so we can check if the account exists in the database
42+
$stmt->store_result();
43+
// Check if the account exists
44+
if ($stmt->num_rows > 0) {
45+
// Username already exists
46+
echo 'Username already exists! Please choose another!';
47+
} else {
48+
// Declare variables
49+
$registered = date('Y-m-d H:i:s');
50+
// We do not want to expose passwords in our database, so hash the password and use password_verify when a user logs in
51+
$password = password_hash($_POST['password'], PASSWORD_DEFAULT);
52+
// Username does not exist, insert new account
53+
if ($stmt = $con->prepare('INSERT INTO accounts (username, password, email, registered) VALUES (?, ?, ?, ?)')) {
54+
// Bind POST data to the prepared statement
55+
$stmt->bind_param('ssss', $_POST['username'], $password, $_POST['email'], $registered);
56+
$stmt->execute();
57+
// Output success message
58+
echo 'You have successfully registered! You can now login!';
59+
} else {
60+
// Something is wrong with the SQL statement, check to make sure the accounts table exists with all 3 fields
61+
echo 'Could not prepare statement!';
62+
}
63+
}
64+
// Close the statement
65+
$stmt->close();
66+
} else {
67+
// Something is wrong with the SQL statement, check to make sure the accounts table exists with all 3 fields.
68+
echo 'Could not prepare statement!';
69+
}
70+
// Close the connection
71+
$con->close();
72+
?>

0 commit comments

Comments
 (0)