How to kill a PHP session?

This common theme appeared earlier, here , here , here , and undoubtedly, elsewhere on the Internet. In my case, unlike them, the hang comes from a blocking socket that never receives messages, and that is probably why the solutions described there did not work for me. I am developing in tandem a C ++ application that communicates with a php script through a local socket connection, and when the C ++ application fails, it leaves the PHP socket to the script waiting for a message that never appears. I tried using session_destroy and session_unset (calling them first in the script before session_start), but they do not work; even crashing and restarting the browser does not help. I can only stop the session if I delete session_start, reload the script, and then end the session through the client. How can I kill a session without going through it?

Edit: I forgot to mention, I also tried to split the socket using

socket_set_option($socket,0, SO_RCVTIMEO, array("sec"=>1, "usec"=>0)); 

But I got a β€œwrong operation” error, and that didn't work.

Edit 2: setting a manual timeout following the prompt here worked quite well. I still don’t know how to kill a session, for example, stuck in an endless loop, but good.

+4
source share
2 answers

Maybe you can give a timeout for the socket? for example socket_select has a timeout parameter.

The value is 1000s , because the wait time may be too high, since apache may have previously killed the process (see max_execution_time in php.ini )

+1
source

How can I kill a session without going through it?

The problem you are facing is not one that will be resolved by killing the session.

the hang comes from a blocking socket that never receives a message

If you use the default PHP session handler, this means that PHP is blocking this file. No other PHP process can manipulate a session as long as this lock exists.

You have several options.

First, consider the implementation of your own session preservation procedures , which means that you have full control over whether there is any lock on session data. You can simply not turn on any lock at all, which allows the running PHP script application to continue to work in peaceful bliss. This is the most difficult option.

Secondly, are you sure a long socket script should write session data? If not, you can simply end the session earlier and release the lock by calling session_write_close . This will release the lock, although in reality it will not end the lengthy script when the socket closes.

Third, look at the use of socket timeouts in your long-term script. I'm not sure which method you use to work with sockets here, and I have very little experience working with them in PHP, so I can not perform a specific function.

+3
source

All Articles