Jump to content

Running code when Session ends


wmeredith

Recommended Posts

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'); ?>
 
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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'); ?>
 
Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.