Page 1 of 1

JpGraph breaks session variables?

Posted: Mon Jun 04, 2007 1:01 am
by Thresher
This question requires some knowledge of JpGraph http://www.aditus.nu/jpgraph/.

Can a session variable exist in both the php file that creates a graph and the php file that calls it?

I want to:
1. set a session variable in the php file that creates the JpGraph and then
2. access that session variable later in the php file that calls the php file that creates the JpGraph.
But its not working.

More specifically, I have 2 php files: graph_harness.php and graph.php. graph_harness.php starts a session, sets a session variable and prints "<img src='graph.php' />" to create the JpGraph. graph.php starts the session, changes the session variable, creates the graph, and completes. graph_harness.php prints out the session variable. The change made by graph.php to the session variable is not evident in the output of graph_harness.php.

graph_harness.php:

Code: Select all

<?php
session_start();
?>
<html><head><title>Graph Harness</title></head><body>
<?php
$_SESSION['MySession'] = 'initial_MySession';

print "<img src='graph.php' /><br />";

print '<p>$_SESSION["MySession"] is: '.$_SESSION['MySession']."</p>\n";
?>
</body></html>
graph.php (simplest graph I could create):

Code: Select all

<?php
session_start();
$_SESSION['MySession'] = 'new_MySession';

include ("/var/www/html/craigraph/jpgraph-2.2/src/jpgraph.php");
include ("/var/www/html/craigraph/jpgraph-2.2/src/jpgraph_bar.php");

// Unix times: Feb 1 2007, Feb 2 2007, Feb 3 2007
$x = array(1172707200, 1172793600, 1172880000);
$y = array(13, 35, 6);

$graph = new Graph(600, 300);
$graph->SetScale('textlin');
$graph->xgrid->Show();

$tickLabels = array();
for ($i = 0; $i < count($x); ++$i)
    $tickLabels[] = date('M d y', $x[$i]);
$graph->xaxis->SetTickLabels($tickLabels);

$plot = new BarPlot($y);
$graph->Add($plot);
$graph->Stroke();
?>
The output is:

Code: Select all

...graph...
$_SESSION["MySession"] is: initial_MySession
The output I wanted is: new_MySession

I used session_id() to verify that both php files have the same session ID.

Is there a way to make this work?

Thanks,
Griff

Re: JpGraph breaks session variables?

Posted: Mon Jun 04, 2007 3:00 am
by volka
Thresher wrote:The change made by graph.php to the session variable is not evident in the output of graph_harness.php.
in graph_harness.php you have $_SESSION['MySession'] = 'initial_MySession';
it unconditionally assigns initial_MySession the array element overwriting a value if there was one.
It's like having

Code: Select all

$a = 'x';
$a = 'y';
echo $a;
and wondering why it prints y instead of x

Posted: Mon Jun 04, 2007 3:29 am
by Thresher
volka,

Code: Select all

<img src='graph.php' />
causes the evaluation of graph.php, which changes the value of $_SESSION['MySession'] and builds the graph. That is the reason the value of the session variable should change. Both graph_harness.php and graph.php report the same session ID, so it should work.

Posted: Mon Jun 04, 2007 7:56 am
by feyd
It's not called by the browser as soon as you echo/print it. So no, on that page request the session data will not change. On the next, it will, but will change back due to graph_harness unconditionally setting the same value.