Just wondering how to check if a PHP session exists... My understanding is that no matter what, if I am using sessions, I have to start my files with session_start() to even acc
I solved this three years ago, but I inadvertently erased the file from my computer.
it went like this. 3 pages that the user had to visit in the order I wanted.
1) top of each php page
enter code here
session start();enter code here
2) first page:
a) enter code here
$_session["timepage1"] = a php date function; time() simple to use
b) enter code here
$_session["timepage2"]= $_session["timepage1"];
b) enter code here
$_session["timepage3"]=$_session["timepage1"];
3) second page:
a) enter code here
$_session["timepage2"] = a php date function; time() simple to use
b) enter code here
$_session["timepage3"]= $_session["timepage3"];
3) third page:
a) enter code here
$_session["timepage3"] = a php date function; time() simple to use
the logic: if timepage3 less than timepage3 on page 2 {the user has gone to page 3 before page 2 do something}
if timepage2 on page 2 less than timepage1 {the user may be trying to hack page two we want them on page 1 do something}
timepage1 should never equal timepage2 or timepage3 on any page except page1 because if it is not greater on pages two or three the user may be trying to hack "do something"
you can do complex things with simple arithmetic with the 3 timepage1-2-3 variables. you can either redirect or send a message to say please go to page 2. you can also tell if user skipped page 2. then send back to page 2 or page one, but best security feature is say nothing just redirect back to page1.
if you enter code here
echo time(); on every page, during testing, you will see the last 3 digits going up if you visit in the correct order.
You can call session_id before session_start. http://www.php.net/manual/en/function.session-id.php - read the id param
In PHP versions prior to 5.4, you can just the session_id() function:
$has_session = session_id() !== '';
In PHP version 5.4+, you can use session_status():
$has_session = session_status() == PHP_SESSION_ACTIVE;
isset($_SESSION)
That should be it. If you wanna check if a single session variable exists, use if(isset($_SESSION['variablename']))
.
I find it best many times (depends on the nature of the application) to simply test to see if a session cookie is set in the client:
<?php
if (isset($_COOKIE["PHPSESSID"])) {
echo "active";
} else {
echo "don't see one";
}
?>
Of course, replace the default session name "PHPSESSID" with any custom one you are using.
switch off the error reporting if noting is working in your php version put top on your php code
error_reporting(0);