note 38601 deleted from function.session-start by tomsommer

From: Date: Sat, 28 Aug 2004 23:00:42 +0000
Subject: note 38601 deleted from function.session-start by tomsommer
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-75517@lists.php.net to get a copy of this message
Note Submitter: webhead at web-feats dot com ---- PRESERVING STATE WHEN CALLING A GD-GENERATED IMAGE How to preserve state when you're doing something like this: File 1: <img src='quadrant_image.php'> File 2: quadrant_image.php quadrant_image.php is dynamically generated using GD2 library. The problem is, File 1 has already loaded before a GET request is issued for File 2, so it's too late for File 1 to set a cookie. <img src='quadrant_image.php?a=$a&b=$b'> would work but what if you want to pass arrays which exceed the 256 character limit for GET? File 2 could pass state back to File 1 by putting File 1 in a frameset coupled with an invisible frame, File 2 writing state to a file, and finally File 1 loading the file that was written, something like this: File 1: <img src='quadrant_image.php' onLoad='parent.invisFrame.location=\"jsArray.htm\"'> Besides being slow, cumbersome and extremely inelegant, this burdens the app with the added complexity of frames. Finally, we can't use $_GLOBALS to pass state as File 1 and File 2 have different scopes. The solution is to use $_SESSION: File 1: session_start(); $_SESSION['num_of_matches'] = mysql_num_rows($result); $_SESSION['quadrant'] = $quadrant; $_SESSION['coords_array'] = $coords_array; print("<img src='quadrant_image.php?" . SID . "'>"); Here, the first session variable is an int, the second a string, the third an array. An added bonus of using sessions is that php handles all serialization for you automagically. Then in File 2: // resume previous session session_start(); $my_num_of_matches = $_SESSION['num_of_matches']; $my_quadrant = $_SESSION['quadrant']; $my_coords_array = $_SESSION['coords_array']; Et voila.

« previous php.notes (#75517) next »