PHP Session Variables In Common Code Sheets and Functions
In my quest to conquer Php sessions, I'm faced with my (lack of)
understanding with Php sessions, and setting \ accessing session
variables.
I wish to place common session variable setting and accessing within a
common php sheet, and execute a function that would set, or reset a
session and its' variables. For example;
common.php (Code sheet)
class SessionSetEnum {const NOT_SET = 0; const CREATED = 1; const
RECREATED = 2;}
function SessionSet($timeOut = 900) {
$ss = SessionSetEnum::NOT_SET;
if(!isset($_SESSION['CREATED'])) {
$_SESSION['CREATED'] = time();
$ss = SessionSetEnum::CREATED;
}
elseif((time() - $_SESSION['CREATED']) > $timeOut) {
session_destroy();
session_unset();
session_regenerate_id(true);
$_SESSION['CREATED'] = time();
$ss = SessionSetEnum::RECREATED;
}
return($ss);
}
For all other sheets on the website, I'll have the following;
index.php (Code sheet)
session_start();
include('common.php');
if (SessionSet() === SessionSetEnum::RECREATED)
{
unset($country_cd);
$country_cd = getCountryCode();
}
another-page.php (Code sheet)
session_start();
include('common.php');
if (SessionSet() === SessionSetEnum::RECREATED)
{
unset($country_cd);
$country_cd = getCountryCode();
}
All examples I've searched, always place session variable setting and
access, directly beneath session_start(), on every code page. I would like
to know if there is anything about php session variables such that, they
should not be coded in a common code sheet?
Please note that I saw on another website or two, the sure way of
destroying a session in php is they way I've expressed it. It does look
like an overkill. Also, the example is not real life, just an example
demonstrating php usage.
Very much appreciate your time and consideration.
Thanks and regards, BotRot
No comments:
Post a Comment