tags:

views:

60

answers:

3

I'm creating a graph that does some database queries then builds the graph using GD functions then also puts the info from the database into an array that is stored in $_SESSION['my_data']. This graph is displayed on the web page using img tags.

<img src="my_graph.php?time=$time">

<?
    print_r($_SESSION['my_data']);
?>

The problem is when I print_r() the array from the session variable it doesn't print the current graphs data; it prints the data that was in the graph last time the page was loaded. It's acting like a cookie.

I have tried putting session_write_close() immediately after I store the array in the session because I thought $_SESSION might have been locked until my_graph.php finished loading the image. That did not work.

I also tried putting sleep(10) before I print the array and that did not work.

Any ideas why this would happen?

+2  A: 

I suppose, when your web server executes PHP code, which draws our page, it already has $_SESSION array initialized. And this array is not updated in current script runtime. When browser finds image tag, it makes another request to web server, which executes image generation script, which updates $_SESSION array in another runtime.

You may:

  • either make all calculations in your web page generation code
  • or reload page after image generation script completes calculations and sets all necessary data in $_SESSION array
Kel
+1  A: 

If you are setting $_SESSION['my_data'] in mygraph.php, you will never see your $_SESSIONchange until your browser requests mygraph.php. This will never occur until the output is flushed to the browser (which will be AFTER you've already print_r() the $_SESSION).

You may be able to try flush() and hope the browser requests the image before you are done executing, I've never tried that (not sure if it will work). Though, sometimes you have to pad output with whitespace until it is about 2k (if I'm not mistaken). I wouldn't recommend this, though.

Another solution would be to request the page you have your code in above in the src. So if your code above is in test.php you could put <img src="test.php?img=true&time=$time">. Then if you get $_GET['img'], display an image, otherwise execute some code. Does that make sense?

Jason
A: 

Sequence of events:

  1. Browser requests the main web page.
  2. PHP runs the main page and sends the contents to the browser.
  3. Browser receives the page.
  4. Browser looks at the page and sends requests to load the graphics, etc.
  5. PHP runs graph.php and sends the graphic to the browser.
  6. Browser inserts the graphic into the page.

The important thing to note is that the whole of point 2 occurs before any of point 5 happens. This means that anything that graph.php does with $_SESSION will not be visible to the code in the main page, leading to the effect you're seeing.

This is the nature of a web page: the graphic files are separate from the main PHP program.

You cannot get around this using a separate graphic file the way you're doing it.

There is one way I can think of achieving it, but it would be a complete rewrite (it's up to you to decide whether it's worth it!)

It is possible to create a graph using Javascript. If you generate the javascript code to do this in the main page, you could then set $_SESSION during the graph generation code, and it would be available later in the program.

If you do decide to do this, you should look into the gRaphael library to help you out.

Hope that helps.

Spudley