Skip to content

Commit b313714

Browse files
author
RichardHpaYoobee
committed
added register, login and logout functions, also added new database
1 parent a28faa4 commit b313714

File tree

6 files changed

+127
-11
lines changed

6 files changed

+127
-11
lines changed

auth/login.php

+26
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,31 @@
11
<?php
22
require '../templates/header.php';
3+
4+
if($_POST){
5+
extract($_POST);
6+
$errors = array();
7+
8+
// validation
9+
10+
if(empty($errors)){
11+
$sql = "SELECT * FROM `users` WHERE username = '$username'";
12+
$result = mysqli_query($dbc, $sql);
13+
if($result && mysqli_affected_rows($dbc) > 0){
14+
$user = mysqli_fetch_array($result, MYSQLI_ASSOC);
15+
if(password_verify($password, $user['password'])){
16+
$_SESSION['valid'] = true;
17+
$_SESSION['timeout'] = time();
18+
$_SESSION['username'] = $user['username'];
19+
header("Location:../index.php");
20+
} else {
21+
array_push($errors, "Incorrect username or password");
22+
}
23+
} else {
24+
array_push($errors, "Sorry there is no username matching that request");
25+
}
26+
27+
}
28+
}
329
?>
430

531
<div class="container py-5">

auth/logout.php

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
require '../templates/header.php';
4+
5+
unset($_SESSION['valid']);
6+
unset($_SESSION['timeout']);
7+
unset($_SESSION['username']);
8+
9+
header("Location: ../index.php");

auth/register.php

+35
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,40 @@
11
<?php
22
require '../templates/header.php';
3+
4+
if($_POST){
5+
extract($_POST);
6+
$errors = array();
7+
8+
//validation
9+
//We need to write an sql query to check to see if the username Email are already there
10+
11+
if(empty($errors)){
12+
$name = mysqli_real_escape_string($dbc, $name);
13+
$username = mysqli_real_escape_string($dbc, $username);
14+
$email = mysqli_real_escape_string($dbc, $email);
15+
16+
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
17+
18+
$sql = "INSERT INTO `users` VALUES (NULL, '$name', '$username', '$email', '$hashedPassword')";
19+
$result = mysqli_query($dbc, $sql);
20+
if($result && mysqli_affected_rows($dbc) > 0){
21+
header("Location: ../index.php");
22+
} else {
23+
array_push($errors, 'Something went wrong, Cannot register user at this time');
24+
}
25+
26+
}
27+
28+
29+
30+
}
31+
32+
33+
34+
35+
36+
37+
338
?>
439

540
<div class="container py-5">

library.sql

+40-5
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
-- http://www.phpmyadmin.net
44
--
55
-- Host: localhost
6-
-- Generation Time: Oct 19, 2018 at 10:36 AM
7-
-- Server version: 5.7.23-0ubuntu0.16.04.1
6+
-- Generation Time: Nov 02, 2018 at 11:58 AM
7+
-- Server version: 5.7.24-0ubuntu0.16.04.1
88
-- PHP Version: 7.2.3-1+ubuntu16.04.1+deb.sury.org+1
99

1010
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
@@ -36,7 +36,8 @@ CREATE TABLE `authors` (
3636
--
3737

3838
INSERT INTO `authors` (`id`, `author_name`) VALUES
39-
(1, 'J. K. Rowling');
39+
(1, 'J. K. Rowling'),
40+
(3, 'Suzanne Collins');
4041

4142
-- --------------------------------------------------------
4243

@@ -52,6 +53,27 @@ CREATE TABLE `books` (
5253
`image_name` varchar(20) NOT NULL DEFAULT 'bookDefault.jpg'
5354
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
5455

56+
--
57+
-- Dumping data for table `books`
58+
--
59+
60+
INSERT INTO `books` (`id`, `book_name`, `author_id`, `description`, `image_name`) VALUES
61+
(20, 'Harry Potter and the Philosopher\'s Stone', 1, 'Harry Potter has been living an ordinary life, constantly abused by his surly and cold aunt and uncle, Vernon and Petunia Dursley and bullied by their spoiled son Dudley since the death of his parents ten years prior. His life changes on the day of his eleventh birthday when he receives a letter of acceptance into a Hogwarts School of Witchcraft and Wizardry, delivered by a half-giant named Rubeus Hagrid after previous letters had been destroyed by Harry’s Uncle Vernon and his Aunt Petunia. Hagrid explains Harry\'s hidden past as the wizard son of James and Lily Potter, who were a wizard and witch respectively, and how they were murdered by the most evil and powerful dark wizard of all time, Lord Voldemort, which resulted in the one-year-old Harry being sent to live with his aunt and uncle. The strangest bit of the murder was how Voldemort was unable to kill him, but instead had his own powers removed and blasted away, sparking Harry\'s immense fame among the magical community.', '5bc903fa6e202.jpg');
62+
63+
-- --------------------------------------------------------
64+
65+
--
66+
-- Table structure for table `users`
67+
--
68+
69+
CREATE TABLE `users` (
70+
`id` tinyint(3) UNSIGNED NOT NULL,
71+
`name` varchar(100) NOT NULL,
72+
`username` varchar(100) NOT NULL,
73+
`email` varchar(100) NOT NULL,
74+
`password` varchar(100) NOT NULL
75+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
76+
5577
--
5678
-- Indexes for dumped tables
5779
--
@@ -70,6 +92,14 @@ ALTER TABLE `books`
7092
ADD UNIQUE KEY `image_name` (`image_name`),
7193
ADD KEY `author_id` (`author_id`);
7294

95+
--
96+
-- Indexes for table `users`
97+
--
98+
ALTER TABLE `users`
99+
ADD PRIMARY KEY (`id`),
100+
ADD UNIQUE KEY `username` (`username`),
101+
ADD UNIQUE KEY `email` (`email`);
102+
73103
--
74104
-- AUTO_INCREMENT for dumped tables
75105
--
@@ -78,12 +108,17 @@ ALTER TABLE `books`
78108
-- AUTO_INCREMENT for table `authors`
79109
--
80110
ALTER TABLE `authors`
81-
MODIFY `id` tinyint(6) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;
111+
MODIFY `id` tinyint(6) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;
82112
--
83113
-- AUTO_INCREMENT for table `books`
84114
--
85115
ALTER TABLE `books`
86-
MODIFY `id` tinyint(6) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=18;
116+
MODIFY `id` tinyint(6) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=21;
117+
--
118+
-- AUTO_INCREMENT for table `users`
119+
--
120+
ALTER TABLE `users`
121+
MODIFY `id` tinyint(3) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;
87122
--
88123
-- Constraints for dumped tables
89124
--

templates/connection.php

+2
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@
1010
die("ERROR, connection couldn't be made, Please check your enviroment files and include the right host, username, password and table.");
1111
}
1212

13+
ob_start();
14+
session_start();
1315
?>

templates/header.php

+15-6
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,21 @@
3737
<div class="col-6 text-center">
3838
<a class="blog-header-logo text-light" href="./">Yoobee School of Design Library</a>
3939
</div>
40-
<div class="col-3 d-flex justify-content-end align-items-center">
41-
<a class="text-light" href="#">
42-
<i class="fas fa-search mx-3"></i>
43-
</a>
44-
<a class="btn btn-sm btn-outline-light" href="./auth/login.php">Sign up</a>
45-
</div>
40+
41+
<div class="col-3 d-flex justify-content-end align-items-center">
42+
<a class="text-light" href="#">
43+
<i class="fas fa-search mx-3"></i>
44+
</a>
45+
<?php if(isset($_SESSION['valid'])): ?>
46+
<a class="btn btn-sm btn-outline-light" href="./"><?= $_SESSION['username']; ?></a>
47+
<a class="btn btn-sm btn-outline-light" href="./auth/logout.php">Logout</a>
48+
<?php else: ?>
49+
<a class="btn btn-sm btn-outline-light" href="./auth/login.php">Sign up</a>
50+
<?php endif; ?>
51+
</div>
52+
53+
54+
4655
</div>
4756
</div>
4857
</header>

0 commit comments

Comments
 (0)