Skip to main content
Code Review

Return to Question

Post Reopened by 200_success, dfhwze, Mast , pacmaninbw , Sᴀᴍ Onᴇᴌᴀ
added 2 characters in body
Source Link
dfhwze
  • 14.1k
  • 3
  • 40
  • 101

Base: /* * Miscellaneous functions */ class Base { public static function location($dir = "index.php") { header("Location: ".$dir); exit(); }

/*
 * Miscellaneous functions
 */
class Base
{
 public static function location($dir = "index.php")
 {
 header("Location: ".$dir);
 exit();
 }
 public static function check_input($required, $error)
 {
 foreach ($required as $field) {
 if (empty($_POST[$field])) {
 Base::location($error);
 }
 }
 }
}

Base: /* * Miscellaneous functions */ class Base { public static function location($dir = "index.php") { header("Location: ".$dir); exit(); }

 public static function check_input($required, $error)
 {
 foreach ($required as $field) {
 if (empty($_POST[$field])) {
 Base::location($error);
 }
 }
 }
}

Base:

/*
 * Miscellaneous functions
 */
class Base
{
 public static function location($dir = "index.php")
 {
 header("Location: ".$dir);
 exit();
 }
 public static function check_input($required, $error)
 {
 foreach ($required as $field) {
 if (empty($_POST[$field])) {
 Base::location($error);
 }
 }
 }
}
Added version 2.0
Source Link

Second Version made after reading the comments What can I do to improve it further

Base: /* * Miscellaneous functions */ class Base { public static function location($dir = "index.php") { header("Location: ".$dir); exit(); }

 public static function check_input($required, $error)
 {
 foreach ($required as $field) {
 if (empty($_POST[$field])) {
 Base::location($error);
 }
 }
 }
}

Session:

/*
 * Session handling class
 */
class Session
{
 public function __construct()
 {
 session_start();
 }
 public function initialize_user_session($admin, $user_id) {
 $_SESSION["admin"] = $admin;
 $_SESSION["loggedIn"] = true;
 $_SESSION["user_id"] = $user_id;
 $_SESSION["csrf_token"] = bin2hex(random_bytes(32));
 }
 public function logout(){
 session_destroy();
 exit();
 }
 public function is_logged_in() {
 return (!empty($_SESSION['logged_in']));
 }
 public function is_admin() {
 return (!empty($_SESSION['admin']));
 }
 /*
 * Check functions
 */
 public function check_token($token, $dir)
 {
 if ($token != $_SESSION["csrf_token"]) {
 Base::location($dir);
 }
 }
 public function check_login($dir)
 {
 if (empty($_SESSION['logged_in'])) {
 Base::location($dir);
 }
 }
 public function check_admin($dir)
 {
 if (empty($_SESSION['admin'])) {
 Base::location($dir);
 }
 }
}

Inpu_Encoding:

/*
 * Functions to prevent XSS
 */
class Input_Encoding
{
 public static function clean_html($html) {
 return htmlspecialchars($html, ENT_QUOTES, 'utf-8');
 }
 public static function clean_json($json) {
 return json_encode($json, JSON_HEX_QUOT|JSON_HEX_TAG|JSON_HEX_AMP|JSON_HEX_APOS);
 }
}

Second Version made after reading the comments What can I do to improve it further

Base: /* * Miscellaneous functions */ class Base { public static function location($dir = "index.php") { header("Location: ".$dir); exit(); }

 public static function check_input($required, $error)
 {
 foreach ($required as $field) {
 if (empty($_POST[$field])) {
 Base::location($error);
 }
 }
 }
}

Session:

/*
 * Session handling class
 */
class Session
{
 public function __construct()
 {
 session_start();
 }
 public function initialize_user_session($admin, $user_id) {
 $_SESSION["admin"] = $admin;
 $_SESSION["loggedIn"] = true;
 $_SESSION["user_id"] = $user_id;
 $_SESSION["csrf_token"] = bin2hex(random_bytes(32));
 }
 public function logout(){
 session_destroy();
 exit();
 }
 public function is_logged_in() {
 return (!empty($_SESSION['logged_in']));
 }
 public function is_admin() {
 return (!empty($_SESSION['admin']));
 }
 /*
 * Check functions
 */
 public function check_token($token, $dir)
 {
 if ($token != $_SESSION["csrf_token"]) {
 Base::location($dir);
 }
 }
 public function check_login($dir)
 {
 if (empty($_SESSION['logged_in'])) {
 Base::location($dir);
 }
 }
 public function check_admin($dir)
 {
 if (empty($_SESSION['admin'])) {
 Base::location($dir);
 }
 }
}

Inpu_Encoding:

/*
 * Functions to prevent XSS
 */
class Input_Encoding
{
 public static function clean_html($html) {
 return htmlspecialchars($html, ENT_QUOTES, 'utf-8');
 }
 public static function clean_json($json) {
 return json_encode($json, JSON_HEX_QUOT|JSON_HEX_TAG|JSON_HEX_AMP|JSON_HEX_APOS);
 }
}
edited title
Link
200_success
  • 145.6k
  • 22
  • 190
  • 479

What is the correct way to structure the following PHP classes with functions into OOP?used for building a web application

New title, examples, and detailed description
Source Link
Loading
Post Closed as "Needs details or clarity" by 200_success, dfhwze, Grajdeanu Alex, pacmaninbw , Dan Oberlam
Added example
Source Link
Loading
Added the comparative review tag.
Link
pacmaninbw
  • 26.1k
  • 13
  • 47
  • 114
Loading
Source Link
Loading
lang-php

AltStyle によって変換されたページ (->オリジナル) /