Detect Hangup client with AGI-PHP

I have an Inbound Route in my asterisk with AGI-PHP scritp to collect customer data. One of my requirements is to limit the number of active calls in the system so I put a numerical variable in a database and it increases every time you answer the call with the script and when it ends, subtract one to that variable. But my problem arises when the client cuts the call before the end of the scritp.
There is a way to detect Hangup client in the Script?