0

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)).

mike rodent
  • 14,126
  • 11
  • 103
  • 157
  • *"I then read that PDO Prepared Statements can't in fact be stored in $_SESSION"* - Huh; how/why is that? – Funk Forty Niner Nov 30 '17 at 17:46
  • Ha, yes, news to me too: https://stackoverflow.com/a/25435734/595305 - guy with a 331k rep: "A PDO object contains state that cannot be represented in the serialization format" – mike rodent Nov 30 '17 at 17:47
  • oh, well that's a different story. Or, is that what you're presently trying to do? The question's a tad unclear. Shouldn't you be posting your code? – Funk Forty Niner Nov 30 '17 at 17:48
  • There's no need for code. The question is about a completely silent error occurring when you try to store something illegal in $_SESSION. – mike rodent Nov 30 '17 at 17:49
  • 1
    Fair enough. Well, we/I don't know whether or not you're using proper error handling. – Funk Forty Niner Nov 30 '17 at 17:51
  • Ah, well, could you explain? I mean that might be worthy of an answer! I have set both `set_error_handler` and `set_exception_handler` ... but I'm no PHP guru, quite obviously. – mike rodent Nov 30 '17 at 17:54
  • That may not be enough. Try both PHP's error reporting set to catch and display http://php.net/manual/en/function.error-reporting.php and PDO error handling http://php.net/manual/en/pdo.error-handling.php - Unless that's what you were referring to. – Funk Forty Niner Nov 30 '17 at 17:57

0 Answers0