From 7cefa3c221d4cad582a4d01e47c9cf149dc360f3 Mon Sep 17 00:00:00 2001 From: Sibidharan <sibidharan@icloud.com> Date: Mon, 7 Feb 2022 15:21:47 +0000 Subject: [PATCH] signup in oops --- _templates/_signup.php | 2 +- libs/includes/Database.class.php | 29 +++++++++++++++++++++++++++++ libs/includes/Mic.class.php | 25 +++++++++++++++++++++++++ libs/includes/User.class.php | 21 +++++++++++++++++++++ libs/load.php | 29 +---------------------------- test.php | 15 ++++++++++++++- 6 files changed, 91 insertions(+), 30 deletions(-) create mode 100644 libs/includes/Database.class.php diff --git a/_templates/_signup.php b/_templates/_signup.php index 2a6c7984..94400eab 100644 --- a/_templates/_signup.php +++ b/_templates/_signup.php @@ -6,7 +6,7 @@ if (isset($_POST['username']) and isset($_POST['password']) and !empty($_POST['p $password = $_POST['password']; $email = $_POST['email_address']; $phone = $_POST['phone']; - $error = signup($username, $password, $email, $phone); + $error = User::signup($username, $password, $email, $phone); $signup = true; } ?> diff --git a/libs/includes/Database.class.php b/libs/includes/Database.class.php new file mode 100644 index 00000000..90e4c78d --- /dev/null +++ b/libs/includes/Database.class.php @@ -0,0 +1,29 @@ +<?php + +class Database +{ + public static $conn = null; + public static function getConnection() + { + if (Database::$conn == null) { + $servername = "mysql.selfmade.ninja"; + $username = "sibidharan"; + $password = "xyjxo8-xefjat-gYnsif"; + $dbname = "sibidharan_newdb"; + + // Create connection + $connection = new mysqli($servername, $username, $password, $dbname); + // Check connection + if ($connection->connect_error) { + die("Connection failed: " . $connection->connect_error); //TODO: Replace this with exception handling + } else { + printf("New connection establishing..."); + Database::$conn = $connection; //replacing null with actual connection + return Database::$conn; + } + } else { + printf("Returning existing establishing..."); + return Database::$conn; + } + } +} diff --git a/libs/includes/Mic.class.php b/libs/includes/Mic.class.php index 2cd09354..2c62fe3a 100644 --- a/libs/includes/Mic.class.php +++ b/libs/includes/Mic.class.php @@ -16,11 +16,18 @@ class Mic public $model; private $light; public $price; + public static $test; + + public static function testFunction() + { + printf("This is a static function, this can be run without object initialization. "); + } public function __construct($brand) { printf("Constructing object..."); $this->brand = ucwords($brand); + Mic::testFunction(); } public function setLight($light) @@ -52,4 +59,22 @@ class Mic { return $this->getModel(); } + + public function __destruct() + { + printf("Destruct object: brand: $this->brand..."); + } +} + +class DupMic +{ + public static function testFunction() + { + return "hello"; + } +} + +function testFunction() +{ + printf("This is a static function, this can be run without object initialization."); } diff --git a/libs/includes/User.class.php b/libs/includes/User.class.php index e69de29b..eee51f5b 100644 --- a/libs/includes/User.class.php +++ b/libs/includes/User.class.php @@ -0,0 +1,21 @@ +<?php + +class User +{ + public static function signup($user, $pass, $email, $phone) + { + $conn = Database::getConnection(); + $sql = "INSERT INTO `auth` (`username`, `password`, `email`, `phone`, `active`) + VALUES ('$user', '$pass', '$email', '$phone', '1');"; + $error = false; + if ($conn->query($sql) === true) { + $error = false; + } else { + // echo "Error: " . $sql . "<br>" . $conn->error; + $error = $conn->error; + } + + // $conn->close(); + return $error; + } +} diff --git a/libs/load.php b/libs/load.php index 9a434999..e9d06e1c 100644 --- a/libs/load.php +++ b/libs/load.php @@ -1,6 +1,7 @@ <?php include_once 'includes/Mic.class.php'; +include_once 'includes/Database.class.php'; function load_template($name) { @@ -15,31 +16,3 @@ function validate_credentials($username, $password) return false; } } - -function signup($user, $pass, $email, $phone) -{ - $servername = "mysql.selfmade.ninja"; - $username = "sibidharan"; - $password = "xyjxo8-xefjat-gYnsif"; - $dbname = "sibidharan_newdb"; - - // Create connection - $conn = new mysqli($servername, $username, $password, $dbname); - // Check connection - if ($conn->connect_error) { - die("Connection failed: " . $conn->connect_error); - } - - $sql = "INSERT INTO `auth` (`username`, `password`, `email`, `phone`, `active`) - VALUES ('$user', '$pass', '$email', '$phone', '1');"; - $error = false; - if ($conn->query($sql) === true) { - $error = false; - } else { - // echo "Error: " . $sql . "<br>" . $conn->error; - $error = $conn->error; - } - - $conn->close(); - return $error; -} diff --git a/test.php b/test.php index f3a02010..7259da6c 100644 --- a/test.php +++ b/test.php @@ -25,6 +25,8 @@ include 'libs/load.php'; $mic1 = new Mic("Roda"); //constructing the object $mic2 = new Mic("HyperX"); //constructing the object +Mic::testFunction(); //no construction, no destruction. +testFunction(); $mic1->setLight("White"); $mic2->setLight("Green"); @@ -36,5 +38,16 @@ print("\n".$mic1->getBrand()); print("\n".$mic2->getBrand()); print("Value of 10+12 is ".$mic1->add(10, 12)); +print("This is mono font inside pre tag \n"); + +$conn = Database::getConnection(); +$conn = Database::getConnection(); +$conn = Database::getConnection(); +$conn = Database::getConnection(); +$conn = Database::getConnection(); +$conn = Database::getConnection(); +$conn = Database::getConnection(); + ?> -</pre> \ No newline at end of file +</pre> +This is regular font. \ No newline at end of file -- GitLab