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);
}
}
}
}
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);
}
}