Controller and Model Session handling classes (MVC OOP PHP)
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
$begingroup$
Model/Session.php
This file performs basic conditional checks and defines a function for each distinct action regarding Session initialization and ID regeneration. Is it OK, in this context, to split the code this much?
<?php
interface SessionInterface {
public function start();
public function isDestroyed();
public function isExpired();
public function wipeSessionVariables();
public function isSetNewSessionID();
public function commitSession();
public function setSessionID();
}
class SessionInit implements SessionInterface {
public function start() {
session_start([
'use_strict_mode' => 1,
'use_only_cookies' => 1,
'use_trans_sid' => 0,
'cookie_httponly' => 1
// 'cookie_samesite' => 1
// 'session.cookie_secure' => 1
]);
}
public function isDestroyed() {
if (isset($_SESSION['destroyed'])) {
return TRUE;
}
return FALSE;
}
public function isExpired() {
if ($_SESSION['destroyed'] < time() - 300) {
return TRUE;
}
return FALSE;
}
public function wipeSessionVariables() {
$_SESSION = array();
}
public function isSetNewSessionID() {
if (isset($_SESSION['new_session_id'])) {
return TRUE;
}
return FALSE;
}
public function commitSession() {
session_commit();
}
public function setSessionID() {
if ($this->isSetNewSessionID() == TRUE) {
session_id($_SESSION['new_session_id']);
}
}
}
interface SessionRegenerateInterface {
public function createNewID();
public function setDestroyed();
public function commitSession();
public function setNewID($newID);
public function initializeSessionID();
public function start();
public function unsetSessionVariables();
}
abstract class AbstractSessionRegenerate implements SessionRegenerateInterface {
public function createNewID() {
$new_session_id = session_create_id();
$_SESSION['new_session_id'] = $new_session_id;
return $this->returnNewSessionID($new_session_id);
}
abstract protected function returnNewSessionID($newID);
}
class SessionRegenerateID extends AbstractSessionRegenerate {
protected function returnNewSessionID($newID) {
return $newID;
}
public function setDestroyed() {
$_SESSION['destroyed'] = time();
}
public function commitSession() {
session_commit();
}
public function setNewID($newID) {
session_id($newID);
}
public function initializeSessionID() {
ini_set('session.use_strict_mode', '0');
session_start([
// without use_strict_mode
'use_only_cookies' => 1,
'use_trans_sid' => 0,
'cookie_httponly' => 1
// 'cookie_samesite' => 1
// 'session.cookie_secure' => 1
]);
}
public function start() {
session_start([
'use_strict_mode' => 1,
'use_only_cookies' => 1,
'use_trans_sid' => 0,
'cookie_httponly' => 1
// 'cookie_samesite' => 1
// 'session.cookie_secure' => 1
]);
}
public function unsetSessionVariables() {
unset($_SESSION['destroyed']);
unset($_SESSION['new_session_id']);
}
}
?>
Controller/Session.php
Based on the results from the Model file, the Controller, which is accessible through the start() and regenerate() functions, will execute the task at hand in the specific order we need, calling a function after the other. This replaces the ugly nested 'if/else' I used to have in my non-OOP approach. Is this good practice?
Another question. Is require "/path";
an acceptable way of using classes from other files?
<?php
require '../Models/Session.php';
interface SessionInitInterface {
public function start();
}
class SessionInitControl implements SessionInitInterface {
public function start() {
$Session = new SessionInit;
$Session->start();
return $this->isDestroyed($Session);
}
protected function isDestroyed($Session) {
if ($Session->isDestroyed() == TRUE) {
$this->isExpired($Session);
}
}
protected function isExpired($Session) {
if ($Session->isExpired() == TRUE) {
$Session->wipeSessionVariables();
throw new Exception('This session is obsolete');
} else {
$this->isSetNewSessionID($Session);
}
}
protected function isSetNewSessionID($Session) {
$Session->commitSession();
$Session->setSessionID();
$Session->start();
}
}
interface SessionRegenerateIDInterface {
public function regenerate();
}
class SessionRegenerateIDControl implements SessionRegenerateIDInterface {
public function regenerate() {
$SessionRegenerate = new SessionRegenerateID;
$newID = $SessionRegenerate->createNewID();
$SessionRegenerate->setDestroyed();
$SessionRegenerate->commitSession();
$SessionRegenerate->setNewID($newID);
$SessionRegenerate->initializeSessionID();
$SessionRegenerate->commitSession();
$SessionRegenerate->start();
$SessionRegenerate->unsetSessionVariables();
}
}
?>
```
php object-oriented mvc
New contributor
$endgroup$
add a comment |
$begingroup$
Model/Session.php
This file performs basic conditional checks and defines a function for each distinct action regarding Session initialization and ID regeneration. Is it OK, in this context, to split the code this much?
<?php
interface SessionInterface {
public function start();
public function isDestroyed();
public function isExpired();
public function wipeSessionVariables();
public function isSetNewSessionID();
public function commitSession();
public function setSessionID();
}
class SessionInit implements SessionInterface {
public function start() {
session_start([
'use_strict_mode' => 1,
'use_only_cookies' => 1,
'use_trans_sid' => 0,
'cookie_httponly' => 1
// 'cookie_samesite' => 1
// 'session.cookie_secure' => 1
]);
}
public function isDestroyed() {
if (isset($_SESSION['destroyed'])) {
return TRUE;
}
return FALSE;
}
public function isExpired() {
if ($_SESSION['destroyed'] < time() - 300) {
return TRUE;
}
return FALSE;
}
public function wipeSessionVariables() {
$_SESSION = array();
}
public function isSetNewSessionID() {
if (isset($_SESSION['new_session_id'])) {
return TRUE;
}
return FALSE;
}
public function commitSession() {
session_commit();
}
public function setSessionID() {
if ($this->isSetNewSessionID() == TRUE) {
session_id($_SESSION['new_session_id']);
}
}
}
interface SessionRegenerateInterface {
public function createNewID();
public function setDestroyed();
public function commitSession();
public function setNewID($newID);
public function initializeSessionID();
public function start();
public function unsetSessionVariables();
}
abstract class AbstractSessionRegenerate implements SessionRegenerateInterface {
public function createNewID() {
$new_session_id = session_create_id();
$_SESSION['new_session_id'] = $new_session_id;
return $this->returnNewSessionID($new_session_id);
}
abstract protected function returnNewSessionID($newID);
}
class SessionRegenerateID extends AbstractSessionRegenerate {
protected function returnNewSessionID($newID) {
return $newID;
}
public function setDestroyed() {
$_SESSION['destroyed'] = time();
}
public function commitSession() {
session_commit();
}
public function setNewID($newID) {
session_id($newID);
}
public function initializeSessionID() {
ini_set('session.use_strict_mode', '0');
session_start([
// without use_strict_mode
'use_only_cookies' => 1,
'use_trans_sid' => 0,
'cookie_httponly' => 1
// 'cookie_samesite' => 1
// 'session.cookie_secure' => 1
]);
}
public function start() {
session_start([
'use_strict_mode' => 1,
'use_only_cookies' => 1,
'use_trans_sid' => 0,
'cookie_httponly' => 1
// 'cookie_samesite' => 1
// 'session.cookie_secure' => 1
]);
}
public function unsetSessionVariables() {
unset($_SESSION['destroyed']);
unset($_SESSION['new_session_id']);
}
}
?>
Controller/Session.php
Based on the results from the Model file, the Controller, which is accessible through the start() and regenerate() functions, will execute the task at hand in the specific order we need, calling a function after the other. This replaces the ugly nested 'if/else' I used to have in my non-OOP approach. Is this good practice?
Another question. Is require "/path";
an acceptable way of using classes from other files?
<?php
require '../Models/Session.php';
interface SessionInitInterface {
public function start();
}
class SessionInitControl implements SessionInitInterface {
public function start() {
$Session = new SessionInit;
$Session->start();
return $this->isDestroyed($Session);
}
protected function isDestroyed($Session) {
if ($Session->isDestroyed() == TRUE) {
$this->isExpired($Session);
}
}
protected function isExpired($Session) {
if ($Session->isExpired() == TRUE) {
$Session->wipeSessionVariables();
throw new Exception('This session is obsolete');
} else {
$this->isSetNewSessionID($Session);
}
}
protected function isSetNewSessionID($Session) {
$Session->commitSession();
$Session->setSessionID();
$Session->start();
}
}
interface SessionRegenerateIDInterface {
public function regenerate();
}
class SessionRegenerateIDControl implements SessionRegenerateIDInterface {
public function regenerate() {
$SessionRegenerate = new SessionRegenerateID;
$newID = $SessionRegenerate->createNewID();
$SessionRegenerate->setDestroyed();
$SessionRegenerate->commitSession();
$SessionRegenerate->setNewID($newID);
$SessionRegenerate->initializeSessionID();
$SessionRegenerate->commitSession();
$SessionRegenerate->start();
$SessionRegenerate->unsetSessionVariables();
}
}
?>
```
php object-oriented mvc
New contributor
$endgroup$
add a comment |
$begingroup$
Model/Session.php
This file performs basic conditional checks and defines a function for each distinct action regarding Session initialization and ID regeneration. Is it OK, in this context, to split the code this much?
<?php
interface SessionInterface {
public function start();
public function isDestroyed();
public function isExpired();
public function wipeSessionVariables();
public function isSetNewSessionID();
public function commitSession();
public function setSessionID();
}
class SessionInit implements SessionInterface {
public function start() {
session_start([
'use_strict_mode' => 1,
'use_only_cookies' => 1,
'use_trans_sid' => 0,
'cookie_httponly' => 1
// 'cookie_samesite' => 1
// 'session.cookie_secure' => 1
]);
}
public function isDestroyed() {
if (isset($_SESSION['destroyed'])) {
return TRUE;
}
return FALSE;
}
public function isExpired() {
if ($_SESSION['destroyed'] < time() - 300) {
return TRUE;
}
return FALSE;
}
public function wipeSessionVariables() {
$_SESSION = array();
}
public function isSetNewSessionID() {
if (isset($_SESSION['new_session_id'])) {
return TRUE;
}
return FALSE;
}
public function commitSession() {
session_commit();
}
public function setSessionID() {
if ($this->isSetNewSessionID() == TRUE) {
session_id($_SESSION['new_session_id']);
}
}
}
interface SessionRegenerateInterface {
public function createNewID();
public function setDestroyed();
public function commitSession();
public function setNewID($newID);
public function initializeSessionID();
public function start();
public function unsetSessionVariables();
}
abstract class AbstractSessionRegenerate implements SessionRegenerateInterface {
public function createNewID() {
$new_session_id = session_create_id();
$_SESSION['new_session_id'] = $new_session_id;
return $this->returnNewSessionID($new_session_id);
}
abstract protected function returnNewSessionID($newID);
}
class SessionRegenerateID extends AbstractSessionRegenerate {
protected function returnNewSessionID($newID) {
return $newID;
}
public function setDestroyed() {
$_SESSION['destroyed'] = time();
}
public function commitSession() {
session_commit();
}
public function setNewID($newID) {
session_id($newID);
}
public function initializeSessionID() {
ini_set('session.use_strict_mode', '0');
session_start([
// without use_strict_mode
'use_only_cookies' => 1,
'use_trans_sid' => 0,
'cookie_httponly' => 1
// 'cookie_samesite' => 1
// 'session.cookie_secure' => 1
]);
}
public function start() {
session_start([
'use_strict_mode' => 1,
'use_only_cookies' => 1,
'use_trans_sid' => 0,
'cookie_httponly' => 1
// 'cookie_samesite' => 1
// 'session.cookie_secure' => 1
]);
}
public function unsetSessionVariables() {
unset($_SESSION['destroyed']);
unset($_SESSION['new_session_id']);
}
}
?>
Controller/Session.php
Based on the results from the Model file, the Controller, which is accessible through the start() and regenerate() functions, will execute the task at hand in the specific order we need, calling a function after the other. This replaces the ugly nested 'if/else' I used to have in my non-OOP approach. Is this good practice?
Another question. Is require "/path";
an acceptable way of using classes from other files?
<?php
require '../Models/Session.php';
interface SessionInitInterface {
public function start();
}
class SessionInitControl implements SessionInitInterface {
public function start() {
$Session = new SessionInit;
$Session->start();
return $this->isDestroyed($Session);
}
protected function isDestroyed($Session) {
if ($Session->isDestroyed() == TRUE) {
$this->isExpired($Session);
}
}
protected function isExpired($Session) {
if ($Session->isExpired() == TRUE) {
$Session->wipeSessionVariables();
throw new Exception('This session is obsolete');
} else {
$this->isSetNewSessionID($Session);
}
}
protected function isSetNewSessionID($Session) {
$Session->commitSession();
$Session->setSessionID();
$Session->start();
}
}
interface SessionRegenerateIDInterface {
public function regenerate();
}
class SessionRegenerateIDControl implements SessionRegenerateIDInterface {
public function regenerate() {
$SessionRegenerate = new SessionRegenerateID;
$newID = $SessionRegenerate->createNewID();
$SessionRegenerate->setDestroyed();
$SessionRegenerate->commitSession();
$SessionRegenerate->setNewID($newID);
$SessionRegenerate->initializeSessionID();
$SessionRegenerate->commitSession();
$SessionRegenerate->start();
$SessionRegenerate->unsetSessionVariables();
}
}
?>
```
php object-oriented mvc
New contributor
$endgroup$
Model/Session.php
This file performs basic conditional checks and defines a function for each distinct action regarding Session initialization and ID regeneration. Is it OK, in this context, to split the code this much?
<?php
interface SessionInterface {
public function start();
public function isDestroyed();
public function isExpired();
public function wipeSessionVariables();
public function isSetNewSessionID();
public function commitSession();
public function setSessionID();
}
class SessionInit implements SessionInterface {
public function start() {
session_start([
'use_strict_mode' => 1,
'use_only_cookies' => 1,
'use_trans_sid' => 0,
'cookie_httponly' => 1
// 'cookie_samesite' => 1
// 'session.cookie_secure' => 1
]);
}
public function isDestroyed() {
if (isset($_SESSION['destroyed'])) {
return TRUE;
}
return FALSE;
}
public function isExpired() {
if ($_SESSION['destroyed'] < time() - 300) {
return TRUE;
}
return FALSE;
}
public function wipeSessionVariables() {
$_SESSION = array();
}
public function isSetNewSessionID() {
if (isset($_SESSION['new_session_id'])) {
return TRUE;
}
return FALSE;
}
public function commitSession() {
session_commit();
}
public function setSessionID() {
if ($this->isSetNewSessionID() == TRUE) {
session_id($_SESSION['new_session_id']);
}
}
}
interface SessionRegenerateInterface {
public function createNewID();
public function setDestroyed();
public function commitSession();
public function setNewID($newID);
public function initializeSessionID();
public function start();
public function unsetSessionVariables();
}
abstract class AbstractSessionRegenerate implements SessionRegenerateInterface {
public function createNewID() {
$new_session_id = session_create_id();
$_SESSION['new_session_id'] = $new_session_id;
return $this->returnNewSessionID($new_session_id);
}
abstract protected function returnNewSessionID($newID);
}
class SessionRegenerateID extends AbstractSessionRegenerate {
protected function returnNewSessionID($newID) {
return $newID;
}
public function setDestroyed() {
$_SESSION['destroyed'] = time();
}
public function commitSession() {
session_commit();
}
public function setNewID($newID) {
session_id($newID);
}
public function initializeSessionID() {
ini_set('session.use_strict_mode', '0');
session_start([
// without use_strict_mode
'use_only_cookies' => 1,
'use_trans_sid' => 0,
'cookie_httponly' => 1
// 'cookie_samesite' => 1
// 'session.cookie_secure' => 1
]);
}
public function start() {
session_start([
'use_strict_mode' => 1,
'use_only_cookies' => 1,
'use_trans_sid' => 0,
'cookie_httponly' => 1
// 'cookie_samesite' => 1
// 'session.cookie_secure' => 1
]);
}
public function unsetSessionVariables() {
unset($_SESSION['destroyed']);
unset($_SESSION['new_session_id']);
}
}
?>
Controller/Session.php
Based on the results from the Model file, the Controller, which is accessible through the start() and regenerate() functions, will execute the task at hand in the specific order we need, calling a function after the other. This replaces the ugly nested 'if/else' I used to have in my non-OOP approach. Is this good practice?
Another question. Is require "/path";
an acceptable way of using classes from other files?
<?php
require '../Models/Session.php';
interface SessionInitInterface {
public function start();
}
class SessionInitControl implements SessionInitInterface {
public function start() {
$Session = new SessionInit;
$Session->start();
return $this->isDestroyed($Session);
}
protected function isDestroyed($Session) {
if ($Session->isDestroyed() == TRUE) {
$this->isExpired($Session);
}
}
protected function isExpired($Session) {
if ($Session->isExpired() == TRUE) {
$Session->wipeSessionVariables();
throw new Exception('This session is obsolete');
} else {
$this->isSetNewSessionID($Session);
}
}
protected function isSetNewSessionID($Session) {
$Session->commitSession();
$Session->setSessionID();
$Session->start();
}
}
interface SessionRegenerateIDInterface {
public function regenerate();
}
class SessionRegenerateIDControl implements SessionRegenerateIDInterface {
public function regenerate() {
$SessionRegenerate = new SessionRegenerateID;
$newID = $SessionRegenerate->createNewID();
$SessionRegenerate->setDestroyed();
$SessionRegenerate->commitSession();
$SessionRegenerate->setNewID($newID);
$SessionRegenerate->initializeSessionID();
$SessionRegenerate->commitSession();
$SessionRegenerate->start();
$SessionRegenerate->unsetSessionVariables();
}
}
?>
```
php object-oriented mvc
php object-oriented mvc
New contributor
New contributor
edited 12 mins ago
alqm
New contributor
asked 22 mins ago
alqmalqm
11
11
New contributor
New contributor
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
alqm is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f217552%2fcontroller-and-model-session-handling-classes-mvc-oop-php%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
alqm is a new contributor. Be nice, and check out our Code of Conduct.
alqm is a new contributor. Be nice, and check out our Code of Conduct.
alqm is a new contributor. Be nice, and check out our Code of Conduct.
alqm is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f217552%2fcontroller-and-model-session-handling-classes-mvc-oop-php%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown