Amiga.org

Coffee House => Coffee House Boards => CH / Science and Technology => Topic started by: motorollin on June 06, 2009, 06:19:21 PM

Title: A PHP problem
Post by: motorollin on June 06, 2009, 06:19:21 PM
I'm having a problem with a web-based application I'm writing. The app requires several PHP session variables. One of the frames contains the following lines which start a check for the variables:

session_start();
require('setup.php');

Here is setup.php:


if( !isset( $_SESSION['size'] ) )
{
$_SESSION['size'] = 32;
}

if( !isset( $_SESSION['map'] ) )
{
$_SESSION['map'] = array();
$_SESSION['map'][] = "...........";
$_SESSION['map'][] = "...........";
$_SESSION['map'][] = "...........";
$_SESSION['map'][] = "...........";
$_SESSION['map'][] = "...........";
$_SESSION['map'][] = "...........";
$_SESSION['map'][] = "...........";
$_SESSION['map'][] = "...........";
$_SESSION['map'][] = "...........";
$_SESSION['map'][] = "...........";
$_SESSION['map'][] = "...........";
}

if( !isset( $_SESSION['undo'] ) )
{
$_SESSION['undo'] = array();
array_push( $_SESSION['undo'], $_SESSION['map'] );
}

//if( !isset( $_SESSION['numbers'] ) )
//{
// $_SESSION['numbers'] = array();
//}

?>

When certain actions are carried out, the function addUndo() is triggered:


function addUndo()
{
array_push( $_SESSION['undo'], $_SESSION['map'] );
}

?>

The problem is that the first time you open the app after loading the browser, the setup appears to work but the next time addUndo() is triggered, the following error is produced:

Quote
Warning: array_push() [function.array-push]: First argument should be an array in /Library/WebServer/Documents/undo.php on line 5

If you reload the page, then the map (produced from $_SESSION['map']) is empty. There is a button on the toolbar to create a new file, which basically destroys the session with session_destroy() and then reloads the main page, which then starts a new session, re-triggers the checks in setup.php, re-creates the session variables, and then it works.

Any ideas why it's not working from the start?

TIA
Title: Re: A PHP problem
Post by: motorollin on June 06, 2009, 06:26:45 PM
I should add that this problem appears to be intermittent...
Title: Re: A PHP problem
Post by: Karlos on June 06, 2009, 07:43:20 PM
Quote from: motorollin;509638
I should add that this problem appears to be intermittent...


What is your session timeout?

I would add that I never use session variables in quite this way, either. My preferred method is to represent any complex session data structures as objects, use them directly and serialize them into the $_SESSION at the end of script execution. When the page next loads, the object is unserialized from the session if it exists, or is constructed from scratch if it doesnt.
Title: Re: A PHP problem
Post by: motorollin on June 07, 2009, 08:37:27 AM
Quote from: Karlos;509647
What is your session timeout?
echo ini_get("session.gc_maxlifetime");
Returns 1440. I assume that's seconds. I'm not really sure why this would make a difference though. Shouldn't the session be destroyed when the browser quits anyway?

Quote from: Karlos;509647
I would add that I never use session variables in quite this way, either. My preferred method is to represent any complex session data structures as objects, use them directly and serialize them into the $_SESSION at the end of script execution. When the page next loads, the object is unserialized from the session if it exists, or is constructed from scratch if it doesnt.
I don't really understand what that means. Would you mind explaining in more detail?