14

I get "Session object destruction failed" when I use session_destroy().

session_start(); if(isset($_SESSION['user_id'])){ $_SESSION=array(); if(isset($_COOKIE[session_name()])){ setcookie(session_name(),'',0,"/"); } session_destroy(); } 

What causes this error?

8
  • 3
    Why do you want to suppress the error, rather than fix it? Commented Dec 18, 2011 at 4:15
  • perhaps because of $_SESSION=array(); or calling session_name() before session_start()? not sure which one is right... Commented Dec 18, 2011 at 4:19
  • Only the first two lines make sense, the rest of the code is baffling. I don't think you understand how $_SESSION works. Just call session_start() and use it. Commented Dec 18, 2011 at 4:21
  • can you post the full error message? Commented Dec 18, 2011 at 4:24
  • 2
    Are you using a custom session handler by any chance? Guessing no or you would have mentioned it in the question, but worth a shot. Commented Dec 18, 2011 at 4:56

4 Answers 4

21

Error:

Warning: session_destroy(): Session object destruction failed

It's rather trivial, no session has been started object has been comitted, so you can't destroy it.

The @ operator is not always active, e.g. with error reporting functions.

Edit:

1) What causes this error?

This error is normally caused when PHP tries to delete the session file, but it can't find it.

In your case with session_destroy there is only one place in PHP which causes this. That's when the session.save_handler (see as well session_set_save_handler) returns FALSE for the destroy action. This can depends which type of save-handler you use, the default one is files. With that one, when the session.save_path setting is wrong (e.g. not an accessible directory), this would cause such an error.

2) Why would the "@" not be suppressing the error?

That depends how the output is created and on PHP configuration. @ does not always work. For example callbacks registered with set_error_handler will still receive these messages.

Sign up to request clarification or add additional context in comments.

12 Comments

There is, at least technically, a session_start() right at the top of the quoted code.
Maybe started, but not yet comitted, see session_commit.
So the answer is to session_write_close() before session_destroy()...?
No, technically not because you can't destroy a session that is not active. See session_destroy. The general problem with the question is, that the @ has been added for some reason and that reason needs to be known to tell what is going on here. Session state can be tricky in edge-cases.
"What is STDOUT?" - STDOUT is Standard Output, in PHP that is what the browser displays. Are you seeing the error message in your browser or inside some log files?
|
1

In my case I was trying to destroy session before cookie was created. In other words I did something like:

session_start(); ... session_destroy(); 

So the server didn't have a chance to 'contact' with the browser before destroying the session. Simple solution that worked for me was

session_start(); ... $_SESSION=array(); 

Comments

0

Use this code:

if(session_status() === PHP_SESSION_ACTIVE) { session_unset(); session_destroy(); } 

Comments

-2

If you are using an autoloader, it may be failing to load a class that is saved in the session.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.