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