From 57097a3c8a51abe516a35b31954f0fbf37693777 Mon Sep 17 00:00:00 2001 From: ui0ppk <113091876+ui0ppk@users.noreply.github.com> Date: Tue, 24 Jan 2023 21:45:10 +0200 Subject: [PATCH] Registering + Config + Code Cleanup --- core/classes.php | 58 ++++++++++++++++++++++++++++++--------- core/classes/user.php | 54 ++++++++++++++++++++++++++++++++++++ core/config.php | 5 ++++ index.php | 4 +-- login.php | 64 +++++++++++++++++++++++++++++++++++++++++++ logout.php | 6 ++++ register.php | 51 ++++++++++++++++++++-------------- 7 files changed, 206 insertions(+), 36 deletions(-) create mode 100644 core/classes/user.php create mode 100644 logout.php diff --git a/core/classes.php b/core/classes.php index 49f00eb..0f329d2 100644 --- a/core/classes.php +++ b/core/classes.php @@ -1,5 +1,10 @@ '; } function buildloggedoutheader() { + require('core/config.php'); + if(isset($_SESSION['user'])) { + $query = $con->prepare('SELECT money FROM users WHERE id=:id'); + $query->bindParam(':id', $id); + $query->execute(); + $money = $query->fetch(PDO::FETCH_ASSOC)["money"]; + + if ($money < 1000000) { + // Anything less than a million + $money = number_format($money); + } else if ($money < 1000000000) { + // Anything less than a billion + $money = number_format($money / 1000000, 1) . 'M'; + } else { + // At least a billion + $money = number_format($money / 1000000000, 1) . 'B'; + + } + // messy shit sorry mario1 fix it urself :trol: + + $rightside = ' '; + } else { + $rightside = ' + + '; + } + echo ''; echo ' '; } -// Contributed by qzip! remove if noob. } ?> \ No newline at end of file diff --git a/core/classes/user.php b/core/classes/user.php new file mode 100644 index 0000000..877a391 --- /dev/null +++ b/core/classes/user.php @@ -0,0 +1,54 @@ +prepare('SELECT * FROM users WHERE id=:id'); + $query->bindParam(':id', $id); + $query->execute(); + $this->sqlData = $query->fetch(PDO::FETCH_ASSOC); + } + + public static function isLoggedIn() { + return isset($_SESSION["user"]); + } + + public function getMoney($short) { + if ($short == "true") { + $money = $this->$query->sqlData["money"]; + + if ($money < 1000000) { + // Anything less than a million + return number_format($money); + } else if ($money < 1000000000) { + // Anything less than a billion + return number_format($money / 1000000, 1) . 'M'; + } else { + // At least a billion + return number_format($money / 1000000000, 1) . 'B'; + + } + } else { + return $this->$query->sqlData["money"]; + } + } + + public function getID($con, $username) { + $query = $con->prepare('SELECT id FROM users WHERE username=:username'); + $query->bindParam(':username', $username); + $query->execute(); + $id = $query->fetch(PDO::FETCH_ASSOC); + return $id['id']; + } + + public function getUsername() { + return $this->sqlData["username"]; + } + + public function getPassword() { + return $this->sqlData["password"]; + } + +} \ No newline at end of file diff --git a/core/config.php b/core/config.php index e0fa65b..e20d82a 100644 --- a/core/config.php +++ b/core/config.php @@ -1,4 +1,9 @@ setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); diff --git a/index.php b/index.php index 964bed1..cd690c9 100644 --- a/index.php +++ b/index.php @@ -25,8 +25,8 @@ body { width: 50%; } -undefined -

undefined

+<?php echo $sitename; ?> +

the funny website with

Register Login
diff --git a/login.php b/login.php index e69de29..0ffc495 100644 --- a/login.php +++ b/login.php @@ -0,0 +1,64 @@ + + + +buildloggedoutheader(); +$passwordiswrong = false; +if(isset($_POST['submit'])) { + $username = $_POST['username']; + $password = $_POST['pass']; + $password = password_hash($password, PASSWORD_DEFAULT); + $user = new User($con, $user->getID($con, $username)); + + $query = $con->prepare('SELECT COUNT(*) FROM users WHERE username=:username'); + $query->bindParam(':username', $username); + $query->execute(); + $result = $query->fetchColumn(); + if ($result > 0) { + if ($password == $user->getPassword($con, $username)) { + $_SESSION["user"] = $user->getID($con, $username); + } else { + $passwordiswrong = true; + echo $password." - sex - ".$user->getPassword($con, $username); + } + } +} +$result = file_get_contents("https://useless-facts.sameerkumar.website/api"); +$thefunfact = json_decode($result); +?> + + + +
+
+

Login

+ +
+ + + + " type="password" placeholder="Your password (Minmum 8 characters)" id="inputSmall" name="pass"> + Password is incorrect.
'; + } ?> +
+
+
+ +
+
Random useless fact generator
+
+

{"data"}; ?>

+
+
+ + \ No newline at end of file diff --git a/logout.php b/logout.php new file mode 100644 index 0000000..382feb0 --- /dev/null +++ b/logout.php @@ -0,0 +1,6 @@ + + + buildloggedoutheader(); $passwordconfirmiswrong = false; $passwordistooshort = false; +$userexists = false; if(isset($_POST['submit'])) { $username = $_POST['username']; $password = $_POST['pass']; $passwordconfirm = $_POST['passconfirm']; - echo "ur username: " . $username; - echo "
"; - echo "ur password: " . $password; - echo "
"; - echo "ur passwordconfirm: " . $passwordconfirm; - echo "
"; + $user = new User($con, $user->getID($con, $username)); if ($password == $passwordconfirm) { $passwordconfirmiswrong = false; } else { @@ -30,37 +29,46 @@ if(isset($_POST['submit'])) { } if ($passwordistooshort == false && $passwordconfirmiswrong == false) { $password = password_hash($password, PASSWORD_DEFAULT); - echo $password; - echo "
"; $query = $con->prepare('SELECT COUNT(*) FROM users WHERE username=:username'); $query->bindParam(':username', $username); $query->execute(); $result = $query->fetchColumn(); if ($result == 0) { - echo "No results, continue. "; $query = $con->prepare('INSERT INTO users (username, password) VALUES (:username, :password)'); $query->bindParam(':username', $username); $query->bindParam(':password', $password); $query->execute(); + $_POST['success'] = 1; + header("Location: /register.php"); } else { - echo "Nope, you stay."; + $userexists = true; } } } +if(isset($_POST['success']) && $_POST['success'] == 1) { +$username = $_POST['username']; +$_SESSION["user"] = $user->getID($con, $username); +} $result = file_get_contents("https://useless-facts.sameerkumar.website/api"); $thefunfact = json_decode($result); ?> -
+ +
-
-

Registration

+
+

Registration

-
- - - +
+ + " type="text" placeholder="Your username" id="inputSmall" for="inputValid" name="username"> + Password Confirmation field is invalid.
'; } ?> + Password is too short. (8 Characters minmum)
'; }?> - - + +
@@ -83,4 +91,5 @@ echo "form-control form-control-sm";

{"data"}; ?>

- + + \ No newline at end of file