On one of my pages I need to add an element to an array each time a submit button is pressed. The array is saved as a session variable so the user can add another element. To see if I can get the idea to work, I have a simplified version of what I need. I have print statements to show me what is in the array each time the page is updated, but nothing prints. I would appreciate anyone looking at the following code and leading me in the right direction.

<?php
// Initialize a session. This call either creates
// a new session or re-establishes an existing one.
session_start( );

// If this is a new session, then the variable
// $count will not be registered and we initialize
if (!session_is_registered("counttest"))
{
$counttest = array();
session_register("counttest");
session_register("count");
session_register("value");
$SESSION['count'] = '1';
$
SESSION['value'] = 'A';
$SESSION["counttest['$count']"] = $value;
}
else
{
$
SESSION['count']++;
$SESSION['value']++;
$
SESSION["counttest[$SESSION[count]"] = $SESSION['value'];
//asort($SESSION['counttest'];
print_r ($
SESSION['counttest']);
foreach($_SESSION['counttest'] as $varname => $value)
print $count . "has a value of ".$value;
}

$sessionId = session_id( );
echo "$sessionId";
echo "<br>";
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Test Session</title>
</head>

<body>

<a href = "testsession.php"> Reload page </a>
</p>
</body>
</html>

Thanks.

Ivan Gepner
gepner@monmouth.edu

    Hello, I tried to comment this the best I could. I know you will learn a thing or two from it 🙂

    First off, you should NEVER use $SESSION and session_register in the same script. Use one or the other, not both. In my example, I did not use $SESSION.

    I'll just post the code, ask me any questions you may have 🙂

    <?php
    //	Initialize Session
    	session_start();
    
    //	Verify Session
    	if (!session_is_registered("counttest"))	{
    	//	Session NOT Registered
    		$counttest	= array();
    		$count		= "1";
    		$value		= "A";
    
    	//	Register Session Variables
    	session_register("counttest","count","value");
    
    	//	Print Current Count & Value
    	echo "Count: " . $count . "<BR>";
    	echo "Value: " . $value . "<BR><BR>";
    
    	//	Create Array with First Values
    	$counttest[] = array($count,$value);
    
    	//	Print Current Array
    	print_r($counttest);
    
    } else {
    
    //	Session Registered
    	$count++;
    	$value++;
    
    	//	Print Current Count & Value
    	echo "Count: " . $count . "<BR>";
    	echo "Value: " . $value . "<BR><BR>";
    
    	//	Add New Values to Current Array
    	$counttest[] = array($count,$value);
    
    	//	Print Current Array
    	print_r($counttest);
    }
    ?>
    

    Regards,
    Matt

      Hi Matt - thanks for answering. In all of the books I've checked no one makes the point that you can't combine $_SESSION and session_start().

      I pasted your code into my page. The body just contains a link to reload the page.

      When I run it I get the following :

      Notice: Undefined variable: count in X:\Websites\Slidetest\testsession.php on line 28

      Notice: Undefined variable: value in X:\Websites\Slidetest\testsession.php on line 29
      Count: 1
      Value: 1

      Array ( [0] => Array ( [0] => 1 [1] => 1 ) )

      So the array is not updating. Any idea why the undefined variable error is popping up?

      Also, this needs to be an associative array with count as the index, a string value of '1', '2', etc. The value variable should be incrementing from A to B to C but somewhere gets changed to 1.

      Thanks again.

      Ivan

        Hi Matt - thanks for answering. In all of the books I've checked no one makes the point that you can't combine $_SESSION and session_start().

        I did not say to not use $SESSION with session_start(), you have no options there. I said, if you use session_register, or session_is_registered you should never use $SESSION and vise versa. I will try to locate some documentation on this.

        I pasted your code into my page. The body just contains a link to reload the page.

        This was my entire code, there is no body. You should not have pasted it anywhere but in a new document.

        Notice: Undefined variable: count in X:\Websites\Slidetest\testsession.php on line 28

        Notice: Undefined variable: value in X:\Websites\Slidetest\testsession.php on line 29
        Count: 1
        Value: 1

        Array ( [0] => Array ( [0] => 1 [1] => 1 ) )

        I would look into your server configuration and make sure that the variable register_globals in your php.ini file is set to ON. session_register does not work without register globals.

        Also, this needs to be an associative array with count as the index, a string value of '1', '2', etc. The value variable should be incrementing from A to B to C but somewhere gets changed to 1.

        This is exactly what it does. Here is the output from running this on my server and pressing the refresh button 9 times:

        Array
        (
            [0] => Array
                (
                    [0] => 1
                    [1] => A
                )
        
        [1] => Array
            (
                [0] => 2
                [1] => B
            )
        
        [2] => Array
            (
                [0] => 3
                [1] => C
            )
        
        [3] => Array
            (
                [0] => 4
                [1] => D
            )
        
        [4] => Array
            (
                [0] => 5
                [1] => E
            )
        
        [5] => Array
            (
                [0] => 6
                [1] => F
            )
        
        [6] => Array
            (
                [0] => 7
                [1] => G
            )
        
        [7] => Array
            (
                [0] => 8
                [1] => H
            )
        
        [8] => Array
            (
                [0] => 9
                [1] => I
            )
        
        [9] => Array
            (
                [0] => 10
                [1] => J
            )
        
        )
        

        There are other ways to build the array, this is merely an example and they way that I prefer. If this doesn't help, i'm sorry, I tried 🙂

        Regards,
        Matt

          Write a Reply...