Friday, February 13, 2009

PHP: Check if PHP sessions are working

Sometimes, there can be some problems with sessions in PHP. In such cases, it is useful to have a very simple and short script that can be used to check if session variables are created, saved and deleted correctly. The following two files can be used:
index.php<?php
session_start();
$_SESSION['user']='tom';
$role = "admin";
session_register('role');
?>
<html>
<body>
Created session variables are:
user='<?=$_SESSION['user']?>' and role='<?=$_SESSION['role']?>'<br><br>
<?='<a href="print.php">Check if session variables are saved</a>';?>
<br><br>
Session ID is: <?= session_id(); ?>

</body>
</html>

print.php<?php
session_start();
if (isset($_GET['delete'])) {
session_unset();
//$_SESSION = array();
session_destroy();

}
?>
<html>
<body>
Saved session variables are:<br><br>
user:<?=$_SESSION['user'];?><br>
role:<?=$_SESSION['role'];?>
<br><br>
Session ID is: <?= session_id(); ?><br><br>
<a href="?delete=1">Delete session</a><br><br>
<a href="index.php">Go back</a>
</body>
</html>
The screenshots are:
index.php:
print.php:
After clicking 'Delete session':

Download

The above scripts can be downloaded from here.

No comments:

Post a Comment