Main Content

Resetting session based tests in PHP

Archive - Originally posted on "The Horse's Mouth" - 2007-08-26 07:23:47 - Graham Ellis

I was writing and testing a PHP session based application - one in which a series of pages are linked together to make up a complete system - yesterday. And as ever with testing, bugs were found in the code and other things had to be added that meant it needed to be changed.

But it's rather different changing a session based applicaion between pages - in essence, you are modifying an application while it is running and that means that variables you need may not be set, and variables may be set when they should not be. And it almost certainly won't start testing back at the beginning.

The first options to solve the issue is to clear out your session cookie - probably called PHPSESSID if the application is in PHP - and start again. But that can be a lot of fiddling with the browser, and can result in other cookies that you would rather retain being lost. Here's an alternative I came up with - my page has a $_SESSION[current] variable through which I remember where I am in the task. And I simply added the "reset line" to my mode below.

if (! $_SESSION[current]) {
  $current = 0;
} else {
  $current = $_SESSION[current];
}
if ($_REQUEST[reset]) $current = 0;


And I can now restart my application (subject to remembering that not all of the old variables may be cleared!) by adding &reset=1 or ?reset=1 onto the end of the test URL.