Welcome, Guest. Please login or register.

Author Topic: A PHP problem  (Read 3278 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline motorollinTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 8669
    • Show only replies by motorollin
A PHP problem
« 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:

<?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:

<?php

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
« Last Edit: June 06, 2009, 06:23:00 PM by motorollin »
Code: [Select]
10  IT\'S THE FINAL COUNTDOWN
20  FOR C = 1 TO 2
30     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NAAAA
40     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NA-NA-NAAAAA
50  NEXT C
60  NA-NA-NAAAA
70  NA-NA NA-NA-NA-NA-NAAAA NAAA-NAAAAAAAAAAA
80  GOTO 10
 

Offline motorollinTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 8669
    • Show only replies by motorollin
Re: A PHP problem
« Reply #1 on: June 06, 2009, 06:26:45 PM »
I should add that this problem appears to be intermittent...
Code: [Select]
10  IT\'S THE FINAL COUNTDOWN
20  FOR C = 1 TO 2
30     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NAAAA
40     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NA-NA-NAAAAA
50  NEXT C
60  NA-NA-NAAAA
70  NA-NA NA-NA-NA-NA-NAAAA NAAA-NAAAAAAAAAAA
80  GOTO 10
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16878
  • Country: gb
  • Thanked: 5 times
    • Show only replies by Karlos
Re: A PHP problem
« Reply #2 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.
int p; // A
 

Offline motorollinTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 8669
    • Show only replies by motorollin
Re: A PHP problem
« Reply #3 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?
Code: [Select]
10  IT\'S THE FINAL COUNTDOWN
20  FOR C = 1 TO 2
30     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NAAAA
40     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NA-NA-NAAAAA
50  NEXT C
60  NA-NA-NAAAA
70  NA-NA NA-NA-NA-NA-NAAAA NAAA-NAAAAAAAAAAA
80  GOTO 10