moved session closing inside the try block to avoid php warning about undefined var