I have just been scratching my head over the use of $_SESSION
, the idea being to make a PDO Prepared Statement during one AJAX call and then retrieve it for subsequent AJAX calls. I couldn't understand why an old value of the $_SESSION
key concerned was seemingly not being replaced by the new value (Prepared Statement)... or rather it was, but only for the duration of the first AJAX call (the one which created the Prepared Statement).
Subsequent AJAX calls not only failed to find that Prepared Statement, but also tended to show that other keys, where the value was a simple string for example, had not been stored either.
I then read that PDO Prepared Statements can't in fact be stored in $_SESSION
... So instead I substituted the SQL query (i.e. the string) ... and stored that in $_SESSION
instead - my problem was solved: I could then retrieve it in the next AJAX call.
What I find puzzling is that there's no hint anywhere that this error (of storing something illegal, thus preventing the key accepting a new value) had occurred. As I say, the first call does allow you to store something illegal, for the duration of that call!
All this actually sent me on a wild goose chase, looking into how PHP sessions work. Does anyone know of a way of detecting this error, so it can be logged/flagged?
NB I have set both set_error_handler
and set_exception_handler
. But this "silent error" rather alarmingly seems to occur between calls: i.e. presumably when the framework or whatever is attempting to serialise the objects it now finds in $_SESSION
... and finds it can't, so just hangs up the phone and, by all appearances, says "nope, I'm going back with the version of $_SESSION
I had before". Might some PHP expert be able to confirm this behaviour?
NB2 regarding levels of error-catching: this same behaviour occurs with maximum PDO error-catching ($dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION)
) and maximum PHP error-catching (error_reporting(E_ALL)
).