From fb24f71956096b3008a99b564453abc80e5e5b90 Mon Sep 17 00:00:00 2001 From: vincent Date: Mon, 14 Apr 2025 15:51:52 +0200 Subject: [PATCH] new database scheme --- auth.php | 4 ++-- login.php | 4 ++-- maintinance.php | 2 +- smz_auth_sessions.sql | 27 +++++++++++++++++++++++++++ smz_auth_users.sql | 27 +++++++++++++++++++++++++++ 5 files changed, 59 insertions(+), 5 deletions(-) create mode 100644 smz_auth_sessions.sql create mode 100644 smz_auth_users.sql diff --git a/auth.php b/auth.php index 7c85aaf..f423ed1 100644 --- a/auth.php +++ b/auth.php @@ -18,7 +18,7 @@ if(!isset($_COOKIE[$cookie_name])) { die("Connection failed: " . $conn->connect_error); } - $sql = "SELECT * FROM smz_sessions WHERE sessionId='$sessionId' AND expiry > ".time()." "; + $sql = "SELECT * FROM smz_auth_sessions WHERE sessionId='$sessionId' AND expiry > ".time()." "; $result = $conn->query($sql); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { @@ -34,7 +34,7 @@ if(!isset($_COOKIE[$cookie_name])) { if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } - $sql = "SELECT email, username FROM smz_users WHERE id='$uid' AND active = 1"; + $sql = "SELECT email, username FROM smz_auth_users WHERE id='$uid' AND active = 1"; $result = $conn->query($sql); $conn->close(); if ($result->num_rows > 0) { diff --git a/login.php b/login.php index 3e17974..bf2fc7d 100644 --- a/login.php +++ b/login.php @@ -13,7 +13,7 @@ $conn = new mysqli($dbservername, $dbusername, $dbpassword, $dbname); if ($conn->connect_error) { die("Server Error"); } -$sql = "SELECT id, password_hash, active FROM smz_users WHERE id='$uid' OR email='$uid'"; +$sql = "SELECT id, password_hash, active FROM smz_auth_users WHERE id='$uid' OR email='$uid'"; $result = $conn->query($sql); $conn->close(); if ($result->num_rows > 0) { @@ -52,7 +52,7 @@ function createSession($uid){ if ($conn->connect_error) { die("Server Error"); } - $sql = "INSERT INTO smz_sessions (sessionId, uid, creation, expiry, active) VALUES ('$sessionID', '$uid', '". time() ."', '$expiry', TRUE)"; + $sql = "INSERT INTO smz_auth_sessions (sessionId, uid, creation, expiry, active) VALUES ('$sessionID', '$uid', '". time() ."', '$expiry', TRUE)"; if ($conn->query($sql) === TRUE) { // echo "New record created successfully"; diff --git a/maintinance.php b/maintinance.php index 41746cb..72b631c 100644 --- a/maintinance.php +++ b/maintinance.php @@ -20,7 +20,7 @@ function cleanSessions(){ } // sql to delete a record - $sql = "DELETE FROM smz_sessions WHERE expiry < ".time()." OR active = 0"; + $sql = "DELETE FROM smz_auth_sessions WHERE expiry < ".time()." OR active = 0"; if ($conn->query($sql) === TRUE) { echo "Record deleted successfully"; diff --git a/smz_auth_sessions.sql b/smz_auth_sessions.sql new file mode 100644 index 0000000..3eef861 --- /dev/null +++ b/smz_auth_sessions.sql @@ -0,0 +1,27 @@ +SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; +SET AUTOCOMMIT = 0; +START TRANSACTION; +SET time_zone = "+00:00"; + +/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; +/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; +/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; +/*!40101 SET NAMES utf8mb4 */; + + +CREATE TABLE `smz_auth_sessions` ( + `sessionId` varchar(32) NOT NULL, + `uid` varchar(128) NOT NULL, + `creation` int(11) NOT NULL, + `expiry` int(11) NOT NULL, + `active` tinyint(1) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_general_ci; + + +ALTER TABLE `smz_auth_sessions` + ADD PRIMARY KEY (`sessionId`); +COMMIT; + +/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; +/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; +/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; diff --git a/smz_auth_users.sql b/smz_auth_users.sql new file mode 100644 index 0000000..9806edc --- /dev/null +++ b/smz_auth_users.sql @@ -0,0 +1,27 @@ +SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; +SET AUTOCOMMIT = 0; +START TRANSACTION; +SET time_zone = "+00:00"; + +/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; +/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; +/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; +/*!40101 SET NAMES utf8mb4 */; + + +CREATE TABLE `smz_auth_users` ( + `id` varchar(128) NOT NULL, + `email` varchar(255) NOT NULL, + `username` varchar(64) NOT NULL, + `password_hash` varchar(255) NOT NULL, + `active` tinyint(1) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_general_ci; + + +ALTER TABLE `smz_auth_users` + ADD PRIMARY KEY (`id`); +COMMIT; + +/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; +/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; +/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;