Last active
July 13, 2020 22:36
-
-
Save Dreller/621e5094871f4836a66662d4d16f378e to your computer and use it in GitHub Desktop.
PHP Session Status for JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Return the status of the current PHP session in a JS readable format. | |
* | |
* Result from session_status() | JS Translation | |
* ------------------------------ --------------------------- | |
* PHP_SESSION_DISABLED 0 | none | |
* PHP_SESSION_NONE 1 | none | |
* PHP_SESSION_ACTIVE 2 | ok | |
* ------------------------------ --------------------------- | |
* | |
* Examples of JSON returned by this function: | |
* {"session": "none"} | |
* {"session": "ok"} | |
* | |
*/ | |
$word = "none"; | |
if( session_status() == PHP_SESSION_ACTIVE ){ | |
$word = "ok"; | |
} | |
// Construct the JSON to be sent. | |
$myJSON = new StdClass; | |
$myJSON->session = $word; | |
// Set header and output the JSON. | |
header('Content-Type: application/json'); | |
echo json_encode($myJSON); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment