Session States

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nikkie
Forum Newbie
Posts: 3
Joined: Tue Jan 06, 2009 8:36 am

Session States

Post by nikkie »

I'm having trouble understanding the behavior of sessions with a particular implementation. The goal is to execute some code that will produce data utilized by both the construction of an image and an image map. The production of the data has a large overhead hence, which is why ideally it should be executed only once and stored in a session variable that can be accessed by both processes. At least that's my reasoning in setting it up as follows:

Code: Select all

<?php
session_start();
require_once "image.dat.php"; 
require_once "image.map.php"; 
?>
<html>
<head>
<title>Image</title>
</head>
<body>
<img border="0" src="image.php" usemap="#imagemap">
<?php echo imagemap(); ?>
</body>
</html>
Therefore, image.dat.php creates the shared data, which are then successfully accessed by both image.php and imagemap() (contained in image.map.php) through a single array variable that is initially set in image.dat.php using $_SESSION['data'] = $data and retrieved using $data = $_SESSION['data'].

But there are a couple of problems. The generated usemap contains links back to the above code, which pass parameters to image.dat.php where they are retrieved with a few $_GET[]s. To further optimize the data construction routines it's desirable to reuse a large portion of the data at this point, which are theoretically contained in $_SESSION['data']. However, when I use the same assignment technique used in image.php and image.map.php ($data = $_SESSION['data']) the code in image.dat.php is somehow executed multiple times resulting in a distorted image and poor performance otherwise. Strangely, if I replace occurrences like $data[THIS][THAT][ETC] with $_SESSION['data'][THIS][THAT][ETC] in image.dat.php everything seems to work fine.

This behavior leads me to believe I don't know what I'm doing. I'm not anxious to replace all occurrences of $data just because it works. Why does it work in image.php and image.map.php but not in image.dat.php? All code and view pages begin with session_start(). What is the server doing in terms of the load and execution sequence that I'm missing?

Additionally, it would be nice to optimize the page load of the above code by allocating the image dimensions with height and width attributes, otherwise there is a highly visible hiccup as the browser double refreshes the screen after computing the height and width. Again, I can't retrieve the $_SESSION variable via assignment without affecting the image as above. However, if I use a non-parented reference e.g. $data[HEIGHT] = $_SESSION['data'][HEIGHT] as opposed to $data = $_SESSION['data'] and then using $data[HEIGHT] the problem goes away. But even when using the $_SESSION['data'][HEIGHT] approach FF does not fully refresh the image. Both IE and Safari show better performance.

Maybe I'm using an invalid approach to begin with. I would appreciate hearing your comments if you have any ideas or questions.
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: Session States

Post by yacahuma »

i did not read the whole thing but if you are calling one thing after the other. you dont need sesssion. the variables will be all global.

Code: Select all

 
$d =0;
require_once 'code_that_change_d_to_1';
require_once 'code_that_change_d_to_3';
echo $d;//=3
 
nikkie
Forum Newbie
Posts: 3
Joined: Tue Jan 06, 2009 8:36 am

Re: Session States

Post by nikkie »

That makes sense and would work for height and width. However, the image.php file is always called as an external file, one that isn't required or included otherwise. It needs to retrieve its data externally and the only way I know how to do that is with a session variable.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Session States

Post by Eran »

Persist the image to a temporary file and store the filename and path in the session.
nikkie
Forum Newbie
Posts: 3
Joined: Tue Jan 06, 2009 8:36 am

Re: Session States

Post by nikkie »

The management of the pictures on disk would be too much.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Session States

Post by Eran »

either delete it immediately after showing, or put in a folder and periodically clean that. Passing image data in the session is not a good idea
Post Reply