Skip to content

Instantly share code, notes, and snippets.

@mallardduck
Forked from lukas-buergi/checkSession.php
Last active March 27, 2020 01:33
Show Gist options
  • Save mallardduck/3f199ccf0a8ce270329bc624de6b26cf to your computer and use it in GitHub Desktop.
Save mallardduck/3f199ccf0a8ce270329bc624de6b26cf to your computer and use it in GitHub Desktop.
Check whether sessions work in php
<?php
error_reporting( E_ALL );
ini_set( 'display_errors', 1);
define('TEST_KEY', 'test');
define('TEST_VAL', 'test42');
$fileName = basename(__FILE__);
if(!isset($_GET['action'])){
die('Param is missing. Look at the <a href="https://gist.github.com/mallardduck/3f199ccf0a8ce270329bc624de6b26cf">source</a>.');
}
switch($_GET['action']) {
case "start":
if(session_start() && $_SESSION[TEST_KEY] = TEST_VAL){
echo 'Test session probably started successfully. Go <a href="' . $fileName . '?action=check">check</a>!</br>';
}else{
echo 'Starting a test session seems to have failed. Go <a href="' . $fileName . '?action=check">check</a>!</br>';
}
echo 'And some additional information:</br>';
echo 'session.save_path : ' . ini_get('session.save_path') . '</br>';
echo 'session.cookie_path : ' . ini_get('session.cookie_path') . '</br>';
echo 'session.name : ' . ini_get('session.name') . '</br>';
break;
case "delete":
if(session_start() && (isset($_SESSION[TEST_KEY]) && $_SESSION[TEST_KEY] === TEST_VAL)){
$_SESSION = array();
if (ini_get("session.use_cookies")) {
$params = session_get_cookie_params();
setcookie(session_name(), '', time() - 42000, $params['path'], $params['domain'], $params['secure'], $params['httponly'] );
}
session_destroy();
echo "Test session deleted.";
}else{
echo 'Sessions don\'t seem to work, or the test session wasn\'t started yet.';
echo 'To start a test session. Go <a href="' . $fileName . '?action=start">check</a>!</br>';
}
break;
case "check":
session_start();
if(isset($_SESSION[TEST_KEY]) && $_SESSION[TEST_KEY] === TEST_VAL){
echo "Sessions seem to work. :-)";
}else{
echo "Sessions don't seem to work. :-(";
}
break;
default: die("Hey, I told you to look at the source. :P");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment