Any comments about the algorithm? Is there a better way to achieve this task? Seems to work for me on both Firefox and IE.
Thanks for you comments.
Code: Select all
<?php
abstract class SessionChecker{
private static $error_number = 0;
public static function check()
{
session_start();
$session_ok = self::checkForSession();
//echo $cookies_ok;
if( $session_ok == 1)
{
return 1;
}
else
{
switch ( $session_ok )
{
case 0:
self::$error_number = 0;
throw new Exception("Cookies Disabled");
break;
case 2:
self::$error_number = 2;
throw new Exception("JavaScript Disabled");
break;
default:
return 0;
break;
}
}
}
private static function checkForSession()
{
if (! isset($_POST['test']) )
{
echo '<script language="JavaScript">
<!--
function submitform()
{
document.sessiontest.submit();
}
// -->
</script>';
echo "<form name=\"sessiontest\" action=\"" . $_SERVER['PHP_SELF'] . " \" method=\"post\">";
echo "<input name=\"test\" type=\"hidden\" value=\"1\" />";
echo "</form>";
$_SESSION['SESSION_OK'] = 1;
echo "<script language=javascript>submitform()</script>";
return 2;
}
else
{
if (isset($_SESSION['SESSION_OK']) && $_SESSION['SESSION_OK'] = 1)
{
return 1;
}
else
return 0;
}
}
public static function getErrorNumber()
{
return self::$error_number;
}
}
?>Code: Select all
try{
SessionChecker::check();
}
catch( Exception $e )
{
system_misconfigured( SessionChecker::getErrorNumber());
}