wmeredith Posted September 18, 2013 Share Posted September 18, 2013 How can you run a query or a piece of code when a session ends or the user exits the browser with out logging out? Thanks. <?php session_start(); include('C:\inetpub\wwwroot\connect.php'); $provider_id = $_SESSION['provider_id']; $sql2 = "INSERT INTO provider_submits (provider_sub) values( '$provider_id')"; $result2 = sqlsrv_query($link, $sql2); exec('c:\\dblocal\\notes.bat'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/282257-running-code-when-session-ends/ Share on other sites More sharing options...
.josh Posted September 18, 2013 Share Posted September 18, 2013 There is a way to run code when the script ends but that's not really what you are looking for, since the script ends when the script finishes executing and the response is given. To do what you want, you need to do it client-side with javascript, which isn't really reliable. Alternatively you could store the session id and info in a database and have a script constantly check against a "last active" timestamp and then put that script on a cronjob to be run every x amount of time. It's not a solution for making it happen immediately, but it's a step up from trying to rely on a client-side solution. Quote Link to comment https://forums.phpfreaks.com/topic/282257-running-code-when-session-ends/#findComment-1450135 Share on other sites More sharing options...
wmeredith Posted September 20, 2013 Author Share Posted September 20, 2013 I need to run a query when the user exits the browser with out properly logging out. Currently when a user logs out there is a query that runs that logs the users entry: $sql2 = "INSERT INTO provider_submits(provider_sub) values( '$provider_id')";$result2 = sqlsrv_query($link, $sql2); [\code] but if the user doesn't logout and exits the browser with out clicking on the logout link the query won't run. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/282257-running-code-when-session-ends/#findComment-1450380 Share on other sites More sharing options...
.josh Posted September 20, 2013 Share Posted September 20, 2013 right. you already mentioned that. and i already responded to that. was there anything in particular you didn't understand about my response? Quote Link to comment https://forums.phpfreaks.com/topic/282257-running-code-when-session-ends/#findComment-1450426 Share on other sites More sharing options...
Psycho Posted September 20, 2013 Share Posted September 20, 2013 As ,josh already stated there is no way to do that directly with any reasonable certainty. A web-page is a stateless type of technology. Even if you implemented javascript, people can turn it off, browsers crash, etc. You have to understand that you can't force the client browser to send anything and figure out how to work around that. I don't know what that piece of information you are trying to save represents. Does it change while the user is navigating the site? If not, can't you save that when the user logs on? But, assuming that value does change, I can think of at least one relatively simple workaround. When the user logs on save a tentative record to the table (e.g. create a new column called 'confirmed' where the default is 0 and another column for the session ID). Then if you need to update the value while the user is logged in use the session ID to do that. Then you can 'confirm' that record when the user logs off. If the user forgets to log off you can do a couple things: 1) The next time the user logs on, see if there are any previous unconfirmed records for the user and confirm them. However, records can remain unconfirmed until the user logs in again which could be week or days. 2) If the above will not meet your needs then create a scheduled job (i.e. CRON Job) that will run every 20 minutes (or whatever will meet your needs) and check for any unconfirmed records that do not have an active session and change them to confirmed. That's just an off the cuff suggestion, but that should give you some avenues to build something to meet your needs. Quote Link to comment https://forums.phpfreaks.com/topic/282257-running-code-when-session-ends/#findComment-1450432 Share on other sites More sharing options...
taquitosensei Posted September 20, 2013 Share Posted September 20, 2013 you could use session_set_save_handler to override session handling then run code when the session is destroyed. How can you run a query or a piece of code when a session ends or the user exits the browser with out logging out? Thanks. <?php session_start(); include('C:\inetpub\wwwroot\connect.php'); $provider_id = $_SESSION['provider_id']; $sql2 = "INSERT INTO provider_submits (provider_sub) values( '$provider_id')"; $result2 = sqlsrv_query($link, $sql2); exec('c:\\dblocal\\notes.bat'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/282257-running-code-when-session-ends/#findComment-1450435 Share on other sites More sharing options...
vinny42 Posted September 20, 2013 Share Posted September 20, 2013 A custom session handler could work, but given that this function itself has nothing to do with the session itself I'd go for a crontab. But, that's assuming that logging out is an event at which you want something critical can happen, especially given the fact that you cannot know when a user logs out (because they don't). I think the OP should probably explain a bit more about what he's doing. Quote Link to comment https://forums.phpfreaks.com/topic/282257-running-code-when-session-ends/#findComment-1450436 Share on other sites More sharing options...
wmeredith Posted September 20, 2013 Author Share Posted September 20, 2013 (edited) Thanks for the responses guys. I added a simple notification at the update page reminding all users to logout correctly and that seems to be working. Edited September 20, 2013 by wmeredith Quote Link to comment https://forums.phpfreaks.com/topic/282257-running-code-when-session-ends/#findComment-1450440 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.