title: How to avoid saving PHP session to disk under certain circumstances
body: <p>I'm working on a quite complex PHP site that store lot of data in the $_SESSION. A standard request involves calling lot of functions that updates the $_SESSION. Exceptions may be thrown at any point of the computation, leaving the data in the $_SESSION inconsistent.</p>
  
  <p>I must avoid inconsistency, data must be saved only if the computation reaches the end. AFAIK PHP writes the session to a file at the end of the script, so if I can prevent it from doing it I should be able to maintain consistency and find the $_SESSION exactly how it was before the crashed call next time I call it, right?</p>
  
  <p>How can I do that?
  I've read that session_set_save_handler() may be the right way to go but I don't want to implement my own way to store the session, I'm fine with how PHP does it, I just want to be able to prevent it under certain circumstances (Exceptions).</p>
  
  <p>Solving the problem by serializing a copy of all data at the beginning of every call to be able to eventually restore it is not an option because of performances (there's quite a lot of data). Also I don't have much control over the whole system so I can't do any "important" refactoring of the code.</p>