Connecting Tech Pros Worldwide Forums | Help | Site Map

session ends

Mikel Astiz
Guest
 
Posts: n/a
#1: Sep 26 '05
Hi,

I am looking for a simple way to detect session ends so I can update a
session table. I am new to PHP and don't understand how such event can be
handled, since there seems not to be a corresponding overridable function.
I've found 'register_shutdown_function', but apparently it detects
connection breaks while the page is being loaded (I'm not sure though).

I supose it will always be possible to control timeouted sessions by
polling, but I wouldn't like to do so. Of course, client side scripts are
not enough. I want to handle broken connections and prevent from evil users
too.

Any ideas will be welcome, and please correct me if I said something
senseless.

Thanks,
Mikel



Erwin Moller
Guest
 
Posts: n/a
#2: Sep 26 '05

re: session ends


Mikel Astiz wrote:
[color=blue]
> Hi,
>
> I am looking for a simple way to detect session ends so I can update a
> session table. I am new to PHP and don't understand how such event can be
> handled, since there seems not to be a corresponding overridable function.
> I've found 'register_shutdown_function', but apparently it detects
> connection breaks while the page is being loaded (I'm not sure though).
>
> I supose it will always be possible to control timeouted sessions by
> polling, but I wouldn't like to do so. Of course, client side scripts are
> not enough. I want to handle broken connections and prevent from evil
> users too.
>
> Any ideas will be welcome, and please correct me if I said something
> senseless.
>
> Thanks,
> Mikel[/color]

If you are using a database to store your sessions, also store in the same
row a field named 'lastupdated'.
Now delete all rows that are not updated for X minutes, where X is something
you think makes sense. (30 minutes eg)

When to run that housekeepingcode?

Why not do it like php does it with file-stored sessions?
Just run it once every 100 times on avarage.
(You can use a random number to decide if you need cleaning.)

Just my 2 cents.

Regards,
Erwin Moller
Colin McKinnon
Guest
 
Posts: n/a
#3: Sep 26 '05

re: session ends


Mikel Astiz wrote:
[color=blue]
> Hi,
>
> I am looking for a simple way to detect session ends so I can update a
> session table. I am new to PHP and don't understand how such event can be
> handled, since there seems not to be a corresponding overridable function.
> I've found 'register_shutdown_function', but apparently it detects
> connection breaks while the page is being loaded (I'm not sure though).
>
> I supose it will always be possible to control timeouted sessions by
> polling, but I wouldn't like to do so. Of course, client side scripts are
> not enough. I want to handle broken connections and prevent from evil
> users too.
>[/color]

RTFM. If a session is not explicitly ended it will be removed by a garbage
collection operation (sessions already have a last active timestamp on
them). If you want to trigger additional events then you'll need to write
your own session handler.

C.
Mikel Astiz
Guest
 
Posts: n/a
#4: Sep 26 '05

re: session ends


That is exactly what I meant with 'polling', but I would also like to
automatically "timeout" sessions even if no queries are made to the server.
Maybe it would be necessary to add another page to do explicit cleanups?

Mikel


"Erwin Moller"
<since_humans_read_this_I_am_spammed_too_much@spam yourself.com> escribió en
el mensaje news:4337b67b$0$11064$e4fe514c@news.xs4all.nl...[color=blue]
> Mikel Astiz wrote:
>[color=green]
>> Hi,
>>
>> I am looking for a simple way to detect session ends so I can update
>> a
>> session table. I am new to PHP and don't understand how such event can be
>> handled, since there seems not to be a corresponding overridable
>> function.
>> I've found 'register_shutdown_function', but apparently it detects
>> connection breaks while the page is being loaded (I'm not sure though).
>>
>> I supose it will always be possible to control timeouted sessions by
>> polling, but I wouldn't like to do so. Of course, client side scripts are
>> not enough. I want to handle broken connections and prevent from evil
>> users too.
>>
>> Any ideas will be welcome, and please correct me if I said something
>> senseless.
>>
>> Thanks,
>> Mikel[/color]
>
> If you are using a database to store your sessions, also store in the same
> row a field named 'lastupdated'.
> Now delete all rows that are not updated for X minutes, where X is
> something
> you think makes sense. (30 minutes eg)
>
> When to run that housekeepingcode?
>
> Why not do it like php does it with file-stored sessions?
> Just run it once every 100 times on avarage.
> (You can use a random number to decide if you need cleaning.)
>
> Just my 2 cents.
>
> Regards,
> Erwin Moller[/color]


Mikel Astiz
Guest
 
Posts: n/a
#5: Sep 26 '05

re: session ends



Write my own session handler? How can I do that? Any references?

Thanks,
Mikel


"Colin McKinnon" <colin.deletethis@andthis.mms3.com> escribió en el mensaje
news:dh8du0$r37$3$8302bc10@news.demon.co.uk...[color=blue]
> Mikel Astiz wrote:
>[color=green]
>> Hi,
>>
>> I am looking for a simple way to detect session ends so I can update
>> a
>> session table. I am new to PHP and don't understand how such event can be
>> handled, since there seems not to be a corresponding overridable
>> function.
>> I've found 'register_shutdown_function', but apparently it detects
>> connection breaks while the page is being loaded (I'm not sure though).
>>
>> I supose it will always be possible to control timeouted sessions by
>> polling, but I wouldn't like to do so. Of course, client side scripts are
>> not enough. I want to handle broken connections and prevent from evil
>> users too.
>>[/color]
>
> RTFM. If a session is not explicitly ended it will be removed by a garbage
> collection operation (sessions already have a last active timestamp on
> them). If you want to trigger additional events then you'll need to write
> your own session handler.
>
> C.[/color]


Erwin Moller
Guest
 
Posts: n/a
#6: Sep 26 '05

re: session ends


Mikel Astiz wrote:
[color=blue]
> That is exactly what I meant with 'polling', but I would also like to
> automatically "timeout" sessions even if no queries are made to the
> server. Maybe it would be necessary to add another page to do explicit
> cleanups?
>
> Mikel[/color]

Hi,

What I do not understand is this: You claim that you store your sessions in
a database, but have no clue how?
(I say this after reading your response to Collin in this thread.)

I mean: PHP stores sessions in files by default, and you have a setup that
stores them in a database: The only way that this could happen is that
somebody changed the sessionhandlers. Must be you. :-)

Are you sure you store your session in a database?

But that aside: To answer your question: when to delete the records?

Well, it depends what you call 'automatic'.
If you write a simple script that only executes one query every hour, is
that automatic? I think so. :-)

So how to do this?
1) Make a script that deletes old rows from your sessiontable.
2) Call this script

WHEN to call the script?
Two 'straight' solutions:
1) Make it a cronjob or something, and execute that job every hour or so.
2) Use your existing sessionhandler, and put the code in there.
But you do not have to clean up with every request to PHP, so that is why I
suggested the 'random-rule', like: do it every 100 requests.

Does this help?

Regards,
Erwin Moller
Oli Filth
Guest
 
Posts: n/a
#7: Sep 27 '05

re: session ends


Mikel Astiz said the following on 26/09/2005 08:45:[color=blue]
> Hi,
>
> I am looking for a simple way to detect session ends so I can update a
> session table. I am new to PHP and don't understand how such event can be
> handled, since there seems not to be a corresponding overridable function.
> I've found 'register_shutdown_function', but apparently it detects
> connection breaks while the page is being loaded (I'm not sure though).
>
> I supose it will always be possible to control timeouted sessions by
> polling, but I wouldn't like to do so. Of course, client side scripts are
> not enough. I want to handle broken connections and prevent from evil users
> too.
>[/color]

You can't handle session timeouts directly, and even if you could, it
would be unreliable/inconsistent as the timeouts are govenered by a
garbage collector.

Why don't you do the cleanup each time the script is run? i.e. assuming
you are keeping track of sessions in a table (with an associated
timestamp), then every time the script is run, look for entries with a
timestamp set too far in the past, and do the relevant cleanup.
(basically what Erwin said in his first reply)


--
Oli
Mikel Astiz
Guest
 
Posts: n/a
#8: Sep 27 '05

re: session ends



Hi Erwin,

I'm afraid I haven't explained myself well (my english doesn't help much
either). I probably didn't explain the difference between PHP sessions and
my own application sessions (client/server architecture).

PHP sessions seem to be automatically handled by a garble collector. I
haven't changed anything there, as far as I know. I do want to handle my own
session mechanism, but I understand this would work over the PHP session.
So, basically, the only problem I have is that I wan't my own session to be
closed (DB updated) as soon as the PHP session is closed or timeouted.

Besides, your last explanation seems to be useful. It hadn't occurred to
me that another mechanism outside PHP (cron) could be used to detect the
invalid session entries and do the cleanup, even if no queries are made to
the server. Given that session ends apparently cannot be processed by some
kind of event handling, your approach seems to be the best alternative.

Thanks a lot,
Mikel

(And speaking about what I told to Collin, it's because I probably don't
understand the term 'session hanlder')


"Erwin Moller"
<since_humans_read_this_I_am_spammed_too_much@spam yourself.com> escribió en
el mensaje news:4337fbc8$0$11075$e4fe514c@news.xs4all.nl...[color=blue]
> Mikel Astiz wrote:
>[color=green]
>> That is exactly what I meant with 'polling', but I would also like to
>> automatically "timeout" sessions even if no queries are made to the
>> server. Maybe it would be necessary to add another page to do explicit
>> cleanups?
>>
>> Mikel[/color]
>
> Hi,
>
> What I do not understand is this: You claim that you store your sessions
> in
> a database, but have no clue how?
> (I say this after reading your response to Collin in this thread.)
>
> I mean: PHP stores sessions in files by default, and you have a setup that
> stores them in a database: The only way that this could happen is that
> somebody changed the sessionhandlers. Must be you. :-)
>
> Are you sure you store your session in a database?
>
> But that aside: To answer your question: when to delete the records?
>
> Well, it depends what you call 'automatic'.
> If you write a simple script that only executes one query every hour, is
> that automatic? I think so. :-)
>
> So how to do this?
> 1) Make a script that deletes old rows from your sessiontable.
> 2) Call this script
>
> WHEN to call the script?
> Two 'straight' solutions:
> 1) Make it a cronjob or something, and execute that job every hour or so.
> 2) Use your existing sessionhandler, and put the code in there.
> But you do not have to clean up with every request to PHP, so that is why
> I
> suggested the 'random-rule', like: do it every 100 requests.
>
> Does this help?
>
> Regards,
> Erwin Moller[/color]


Geoff Berrow
Guest
 
Posts: n/a
#9: Sep 27 '05

re: session ends


I noticed that Message-ID: <dhb27v$6ld$1@localhost.localdomain> from
Mikel Astiz contained the following:
[color=blue]
> PHP sessions seem to be automatically handled by a garble collector.[/color]

Love it! It's actually 'garbage collector' but given the nature of a
session id, I prefer your version.
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Tony Marston
Guest
 
Posts: n/a
#10: Oct 1 '05

re: session ends


Taken a look at http://www.tonymarston.co.uk/php-mys...n-handler.html

Or RTFM at http://uk.php.net/manual/en/function...ve-handler.php

--
Tony Marston

http://www.tonymarston.net


"Mikel Astiz" <mastiz001@ikasle.ehu.es> wrote in message
news:dh8nd3$qa$1@localhost.localdomain...[color=blue]
>
> Write my own session handler? How can I do that? Any references?
>
> Thanks,
> Mikel
>
>
> "Colin McKinnon" <colin.deletethis@andthis.mms3.com> escribió en el
> mensaje news:dh8du0$r37$3$8302bc10@news.demon.co.uk...[color=green]
>> Mikel Astiz wrote:
>>[color=darkred]
>>> Hi,
>>>
>>> I am looking for a simple way to detect session ends so I can update
>>> a
>>> session table. I am new to PHP and don't understand how such event can
>>> be
>>> handled, since there seems not to be a corresponding overridable
>>> function.
>>> I've found 'register_shutdown_function', but apparently it detects
>>> connection breaks while the page is being loaded (I'm not sure though).
>>>
>>> I supose it will always be possible to control timeouted sessions by
>>> polling, but I wouldn't like to do so. Of course, client side scripts
>>> are
>>> not enough. I want to handle broken connections and prevent from evil
>>> users too.
>>>[/color]
>>
>> RTFM. If a session is not explicitly ended it will be removed by a
>> garbage
>> collection operation (sessions already have a last active timestamp on
>> them). If you want to trigger additional events then you'll need to write
>> your own session handler.
>>
>> C.[/color]
>
>[/color]


Closed Thread