The following class is set of functions I use for web development(examples below) and I was wondering how to structure it.
Examples:
//Checking if user isn't admin so that he can access login page
$helper = new Base();
if ($helper->is_admin()) {
$helper->location("index.php");
}
//Encoding output to prevent XSS
$html = "<script>alert("XSS")</script>";
echo "<h1>". $helper->clean_html($html) ."</h1>";
//Redirect to another webpage and exit
$helper->location("error.php");
Is having just one class for all simple functions the correct way?
<?php
class Base
{
public function __construct()
{
session_start();
}
public function location($dir = "index.php")
{
header("Location: ".$dir);
exit();
}
public function is_logged_in() {
return (isset($_SESSION['logged_in']) && $_SESSION['logged_in']);
}
public function is_admin() {
return (isset($_SESSION['admin']) && $_SESSION['admin']);
}
/*
* Clean functions para prevenir XSS
*/
public function clean_html($html) {
return htmlspecialchars($html, ENT_QUOTES, 'utf-8');
}
public function clean_json($json) {
return json_encode($json, JSON_HEX_QUOT|JSON_HEX_TAG|JSON_HEX_AMP|JSON_HEX_APOS);
}
/*
* Check functions
*/
public function check_token($token, $dir)
{
if ($token != $_SESSION["csrf_token"]) {
$this->location($dir);
}
}
public function check_login($dir)
{
if (!$this->is_logged_in()) {
$this->location($dir);
}
}
public function check_admin($dir)
{
if (!$this->is_admin()) {
$this->location($dir);
}
}
public function check_input($required, $erro)
{
foreach ($required as $field) {
if (!empty($_POST[$field])) {
$this->location($erro);
}
}
}
}
Or since the check functions build on previous functions should I structure it the following way:
Base class:
<?php
class Base
{
public function __construct()
{
session_start();
}
public function location($dir = "index.php")
{
header("Location: ".$dir);
exit();
}
public function is_logged_in() {
return (isset($_SESSION['logged_in']) && $_SESSION['logged_in']);
}
public function is_admin() {
return (isset($_SESSION['admin']) && $_SESSION['admin']);
}
/*
* Clean functions para prevenir XSS
*/
public function clean_html($html) {
return htmlspecialchars($html, ENT_QUOTES, 'utf-8');
}
public function clean_json($json) {
return json_encode($json, JSON_HEX_QUOT|JSON_HEX_TAG|JSON_HEX_AMP|JSON_HEX_APOS);
}
}
And
Helper class:
<?php
class Helper extends Base
{
protected $base;
public function __construct()
{
$this->base = new Base;
}
public function check_token($token, $dir)
{
if ($token != $_SESSION["csrf_token"]) {
$this->base->location($dir);
}
}
public function check_login($dir)
{
if (!$this->base->is_logged_in()) {
$this->base->location($dir);
}
}
public function check_admin($dir)
{
if (!$this->base->is_admin()) {
$this->base->location($dir);
}
}
public function check_input($required, $erro)
{
foreach ($required as $field) {
if (!empty($_POST[$field])) {
$this->base->location($erro);
}
}
}
}
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);
}
}
session_start()
defined? Is this working code, or is it hypothetical? We can only review working code on code review. Hypothetical questions are off-topic and may be closed. Please see our guidelines at codereview.stackexchange.com/help/dont-ask and codereview.stackexchange.com/help/how-to-ask. \$\endgroup\$ – pacmaninbw Sep 8 '19 at 14:42