Skip to content

Instantly share code, notes, and snippets.

@Dreller
Last active July 13, 2020 22:36
Show Gist options
  • Save Dreller/621e5094871f4836a66662d4d16f378e to your computer and use it in GitHub Desktop.
Save Dreller/621e5094871f4836a66662d4d16f378e to your computer and use it in GitHub Desktop.
PHP Session Status for JS
<?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