Discussion:
how to see all sessions sets in server
Farzan Dalaee
2013-08-04 09:02:55 UTC
Permalink
hi
i want to write online user module for my site and i want to check
$_SESSION['userID'] to find all users id who loged in
but when i echo this code its return only current user detail
how i can see all sessions?

foreach($_SESSION as $k => $v)
{
echo $k."----------".$v;
}
or how i handle online users?
Alessandro Pellizzari
2013-08-04 10:13:03 UTC
Permalink
Il Sun, 04 Aug 2013 13:32:55 +0430, Farzan Dalaee ha scritto:

> hi i want to write online user module for my site and i want to check
> $_SESSION['userID'] to find all users id who loged in but when i echo
> this code its return only current user detail how i can see all
> sessions?

You can't.

> or how i handle online users?

Every user has its session.

If you want to have a "super user" who has access to all the sessions,
you can use the filesystem functions to read the directory in which the
sessions get saved (it depends on the server configuration) or you can
implement a session handler to save all the sessions in the database, and
give access to that table to one user.

Bye.



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Matijn Woudt
2013-08-04 10:14:49 UTC
Permalink
On Sun, Aug 4, 2013 at 11:02 AM, Farzan Dalaee <***@gmail.com>wrote:

> hi
> i want to write online user module for my site and i want to check
> $_SESSION['userID'] to find all users id who loged in
> but when i echo this code its return only current user detail
> how i can see all sessions?
>
> foreach($_SESSION as $k => $v)
> {
> echo $k."----------".$v;
> }
> or how i handle online users?
>

You can only access sessions when you know the session id.
Most sites handle online users in their database, store a timestamp each
time a user loads a page. When you want to display the online users, check
where the timestamp is between now and a few minutes ago. Note that without
javascript (or flash/java/etc) there is no way to truly know if the user
left or not. The session will stay active for a long time, depending on
your php.ini settings.

- Matijn
Farzan Dalaee
2013-08-04 10:26:29 UTC
Permalink
You mean when user logged in i add new record to table and when logged out i delete the row? So if user close the browser without logout how can i find user is online or not?

Sent from my iPhone

On Aug 4, 2013, at 14:44, Matijn Woudt <***@gmail.com> wrote:

>
>
>
> On Sun, Aug 4, 2013 at 11:02 AM, Farzan Dalaee <***@gmail.com> wrote:
>> hi
>> i want to write online user module for my site and i want to check
>> $_SESSION['userID'] to find all users id who loged in
>> but when i echo this code its return only current user detail
>> how i can see all sessions?
>>
>> foreach($_SESSION as $k => $v)
>> {
>> echo $k."----------".$v;
>> }
>> or how i handle online users?
>
> You can only access sessions when you know the session id.
> Most sites handle online users in their database, store a timestamp each time a user loads a page. When you want to display the online users, check where the timestamp is between now and a few minutes ago. Note that without javascript (or flash/java/etc) there is no way to truly know if the user left or not. The session will stay active for a long time, depending on your php.ini settings.
>
> - Matijn
>
Ashley Sheridan
2013-08-04 10:28:05 UTC
Permalink
On Sun, 2013-08-04 at 14:56 +0430, Farzan Dalaee wrote:

> You mean when user logged in i add new record to table and when logged out i delete the row? So if user close the browser without logout how can i find user is online or not?
>
> Sent from my iPhone
>
> On Aug 4, 2013, at 14:44, Matijn Woudt <***@gmail.com> wrote:
>
> >
> >
> >
> > On Sun, Aug 4, 2013 at 11:02 AM, Farzan Dalaee <***@gmail.com> wrote:
> >> hi
> >> i want to write online user module for my site and i want to check
> >> $_SESSION['userID'] to find all users id who loged in
> >> but when i echo this code its return only current user detail
> >> how i can see all sessions?
> >>
> >> foreach($_SESSION as $k => $v)
> >> {
> >> echo $k."----------".$v;
> >> }
> >> or how i handle online users?
> >
> > You can only access sessions when you know the session id.
> > Most sites handle online users in their database, store a timestamp each time a user loads a page. When you want to display the online users, check where the timestamp is between now and a few minutes ago. Note that without javascript (or flash/java/etc) there is no way to truly know if the user left or not. The session will stay active for a long time, depending on your php.ini settings.
> >
> > - Matijn
> >


Like Matijn said, unless you're using some kind of client-side method to
continually poll the server, you can't know if they've just closed their
browser. There are Javascript events for exiting a page, but they don't
work correctly on Safari and iOS Safari.

You don't have to actually save anything to the DB manually, just
instruct PHP to use the DB for its own sessions, rather than files.

Do you really need to inspect each visitors session in detail, or do you
just need a way to determine how many unique visitors are on the site at
any one time?

Thanks,
Ash
http://www.ashleysheridan.co.uk
Farzan Dalaee
2013-08-04 13:36:30 UTC
Permalink
> On Sun, 2013-08-04 at 14:56 +0430, Farzan Dalaee wrote:
>>
>> You mean when user logged in i add new record to table and when logged out i delete the row? So if user close the browser without logout how can i find user is online or not?
>>
>> Sent from my iPhone
>>
>> On Aug 4, 2013, at 14:44, Matijn Woudt <***@gmail.com> wrote:
>>
>> >
>> >
>> >
>> > On Sun, Aug 4, 2013 at 11:02 AM, Farzan Dalaee <***@gmail.com> wrote:
>> >> hi
>> >> i want to write online user module for my site and i want to check
>> >> $_SESSION['userID'] to find all users id who loged in
>> >> but when i echo this code its return only current user detail
>> >> how i can see all sessions?
>> >>
>> >> foreach($_SESSION as $k => $v)
>> >> {
>> >> echo $k."----------".$v;
>> >> }
>> >> or how i handle online users?
>> >
>> > You can only access sessions when you know the session id.
>> > Most sites handle online users in their database, store a timestamp each time a user loads a page. When you want to display the online users, check where the timestamp is between now and a few minutes ago. Note that without javascript (or flash/java/etc) there is no way to truly know if the user left or not. The session will stay active for a long time, depending on your php.ini settings.
>> >
>> > - Matijn
>> >
>
> Like Matijn said, unless you're using some kind of client-side method to continually poll the server, you can't know if they've just closed their browser. There are Javascript events for exiting a page, but they don't work correctly on Safari and iOS Safari.
>
> You don't have to actually save anything to the DB manually, just instruct PHP to use the DB for its own sessions, rather than files.
>
> Do you really need to inspect each visitors session in detail, or do you just need a way to determine how many unique visitors are on the site at any one time?
>
> Thanks,
> Ash
> http://www.ashleysheridan.co.uk
>
>
>


I need to inspect each visitor to show how online or who offline for chat
Like facebook chat
>
Stuart Dallas
2013-08-04 13:59:31 UTC
Permalink
On 4 Aug 2013, at 14:36, Farzan Dalaee <***@gmail.com> wrote:

> I need to inspect each visitor to show how online or who offline for chat
> Like facebook chat

PHP sessions is a really bad mechanism to use to accomplish this. In the past I've used memcached or redis for presence indicators. Both allow you to set keys that expire after a set period. The problem with this is that a user can appear to be online for up to "set period" seconds longer than they actually are.

In the system I'm currently building I've got a node.js daemon to which the browser connects. That daemon exposes an API that the rest of the system can call to get lists of online users, which is easily gathered since it's just the list of connected users. There is then no delay between the browser closing the connection and the daemon deciding that user is no longer online.

If you have a chat system I'm curious as to how it works, since presence is usually tied closely to how the client is detecting new messages.

-Stuart

--
Stuart Dallas
3ft9 Ltd
http://3ft9.com/
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Ashley Sheridan
2013-08-04 14:00:38 UTC
Permalink
Farzan Dalaee <***@gmail.com> wrote:
>
>
>> On Sun, 2013-08-04 at 14:56 +0430, Farzan Dalaee wrote:
>>>
>>> You mean when user logged in i add new record to table and when
>logged out i delete the row? So if user close the browser without
>logout how can i find user is online or not?
>>>
>>> Sent from my iPhone
>>>
>>> On Aug 4, 2013, at 14:44, Matijn Woudt <***@gmail.com> wrote:
>>>
>>> >
>>> >
>>> >
>>> > On Sun, Aug 4, 2013 at 11:02 AM, Farzan Dalaee
><***@gmail.com> wrote:
>>> >> hi
>>> >> i want to write online user module for my site and i want to
>check
>>> >> $_SESSION['userID'] to find all users id who loged in
>>> >> but when i echo this code its return only current user detail
>>> >> how i can see all sessions?
>>> >>
>>> >> foreach($_SESSION as $k => $v)
>>> >> {
>>> >> echo $k."----------".$v;
>>> >> }
>>> >> or how i handle online users?
>>> >
>>> > You can only access sessions when you know the session id.
>>> > Most sites handle online users in their database, store a
>timestamp each time a user loads a page. When you want to display the
>online users, check where the timestamp is between now and a few
>minutes ago. Note that without javascript (or flash/java/etc) there is
>no way to truly know if the user left or not. The session will stay
>active for a long time, depending on your php.ini settings.
>>> >
>>> > - Matijn
>>> >
>>
>> Like Matijn said, unless you're using some kind of client-side method
>to continually poll the server, you can't know if they've just closed
>their browser. There are Javascript events for exiting a page, but they
>don't work correctly on Safari and iOS Safari.
>>
>> You don't have to actually save anything to the DB manually, just
>instruct PHP to use the DB for its own sessions, rather than files.
>>
>> Do you really need to inspect each visitors session in detail, or do
>you just need a way to determine how many unique visitors are on the
>site at any one time?
>>
>> Thanks,
>> Ash
>> http://www.ashleysheridan.co.uk
>>
>>
>>
>
>
>I need to inspect each visitor to show how online or who offline for
>chat
>Like facebook chat
>>

Ah, so you don't need to see the details of the sessions then. Facebook does this (badly) by using javascript on the client side which triggers an update of a timestamp on the server, which then allows you to determine who is online (or was within a given time limit)
Thanks,
Ash

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Matijn Woudt
2013-08-04 15:54:52 UTC
Permalink
On Sun, Aug 4, 2013 at 4:00 PM, Ashley Sheridan <***@ashleysheridan.co.uk>wrote:

>
>
> Farzan Dalaee <***@gmail.com> wrote:
> >
> >
> >> On Sun, 2013-08-04 at 14:56 +0430, Farzan Dalaee wrote:
> >>>
> >>> You mean when user logged in i add new record to table and when
> >logged out i delete the row? So if user close the browser without
> >logout how can i find user is online or not?
> >>>
> >>> Sent from my iPhone
> >>>
> >>> On Aug 4, 2013, at 14:44, Matijn Woudt <***@gmail.com> wrote:
> >>>
> >>> >
> >>> >
> >>> >
> >>> > On Sun, Aug 4, 2013 at 11:02 AM, Farzan Dalaee
> ><***@gmail.com> wrote:
> >>> >> hi
> >>> >> i want to write online user module for my site and i want to
> >check
> >>> >> $_SESSION['userID'] to find all users id who loged in
> >>> >> but when i echo this code its return only current user detail
> >>> >> how i can see all sessions?
> >>> >>
> >>> >> foreach($_SESSION as $k => $v)
> >>> >> {
> >>> >> echo $k."----------".$v;
> >>> >> }
> >>> >> or how i handle online users?
> >>> >
> >>> > You can only access sessions when you know the session id.
> >>> > Most sites handle online users in their database, store a
> >timestamp each time a user loads a page. When you want to display the
> >online users, check where the timestamp is between now and a few
> >minutes ago. Note that without javascript (or flash/java/etc) there is
> >no way to truly know if the user left or not. The session will stay
> >active for a long time, depending on your php.ini settings.
> >>> >
> >>> > - Matijn
> >>> >
> >>
> >> Like Matijn said, unless you're using some kind of client-side method
> >to continually poll the server, you can't know if they've just closed
> >their browser. There are Javascript events for exiting a page, but they
> >don't work correctly on Safari and iOS Safari.
> >>
> >> You don't have to actually save anything to the DB manually, just
> >instruct PHP to use the DB for its own sessions, rather than files.
> >>
> >> Do you really need to inspect each visitors session in detail, or do
> >you just need a way to determine how many unique visitors are on the
> >site at any one time?
> >>
> >> Thanks,
> >> Ash
> >> http://www.ashleysheridan.co.uk
> >>
> >>
> >>
> >
> >
> >I need to inspect each visitor to show how online or who offline for
> >chat
> >Like facebook chat
> >>
>
> Ah, so you don't need to see the details of the sessions then. Facebook
> does this (badly) by using javascript on the client side which triggers an
> update of a timestamp on the server, which then allows you to determine who
> is online (or was within a given time limit)
> Thanks,
> Ash
>

Maybe it's bad, but there's no good alternative, except sending ping
requests to your server every second or so, but any site as large as
Facebook will DDOS itself when using things like that ;)

- Matijn
Farzan Dalaee
2013-08-04 16:17:37 UTC
Permalink
>
>
>
> On Sun, Aug 4, 2013 at 4:00 PM, Ashley Sheridan <***@ashleysheridan.co.uk> wrote:
>>
>>
>> Farzan Dalaee <***@gmail.com> wrote:
>> >
>> >
>> >> On Sun, 2013-08-04 at 14:56 +0430, Farzan Dalaee wrote:
>> >>>
>> >>> You mean when user logged in i add new record to table and when
>> >logged out i delete the row? So if user close the browser without
>> >logout how can i find user is online or not?
>> >>>
>> >>> Sent from my iPhone
>> >>>
>> >>> On Aug 4, 2013, at 14:44, Matijn Woudt <***@gmail.com> wrote:
>> >>>
>> >>> >
>> >>> >
>> >>> >
>> >>> > On Sun, Aug 4, 2013 at 11:02 AM, Farzan Dalaee
>> ><***@gmail.com> wrote:
>> >>> >> hi
>> >>> >> i want to write online user module for my site and i want to
>> >check
>> >>> >> $_SESSION['userID'] to find all users id who loged in
>> >>> >> but when i echo this code its return only current user detail
>> >>> >> how i can see all sessions?
>> >>> >>
>> >>> >> foreach($_SESSION as $k => $v)
>> >>> >> {
>> >>> >> echo $k."----------".$v;
>> >>> >> }
>> >>> >> or how i handle online users?
>> >>> >
>> >>> > You can only access sessions when you know the session id.
>> >>> > Most sites handle online users in their database, store a
>> >timestamp each time a user loads a page. When you want to display the
>> >online users, check where the timestamp is between now and a few
>> >minutes ago. Note that without javascript (or flash/java/etc) there is
>> >no way to truly know if the user left or not. The session will stay
>> >active for a long time, depending on your php.ini settings.
>> >>> >
>> >>> > - Matijn
>> >>> >
>> >>
>> >> Like Matijn said, unless you're using some kind of client-side method
>> >to continually poll the server, you can't know if they've just closed
>> >their browser. There are Javascript events for exiting a page, but they
>> >don't work correctly on Safari and iOS Safari.
>> >>
>> >> You don't have to actually save anything to the DB manually, just
>> >instruct PHP to use the DB for its own sessions, rather than files.
>> >>
>> >> Do you really need to inspect each visitors session in detail, or do
>> >you just need a way to determine how many unique visitors are on the
>> >site at any one time?
>> >>
>> >> Thanks,
>> >> Ash
>> >> http://www.ashleysheridan.co.uk
>> >>
>> >>
>> >>
>> >
>> >
>> >I need to inspect each visitor to show how online or who offline for
>> >chat
>> >Like facebook chat
>> >>
>>
>> Ah, so you don't need to see the details of the sessions then. Facebook does this (badly) by using javascript on the client side which triggers an update of a timestamp on the server, which then allows you to determine who is online (or was within a given time limit)
>> Thanks,
>> Ash
>
> Maybe it's bad, but there's no good alternative, except sending ping requests to your server every second or so, but any site as large as Facebook will DDOS itself when using things like that ;)
>
> - Matijn

So best way is use a script(javascript) to send ajax to server every 5 second to check users is logged in or not? Is that okey?
I want to write chat module like facebook and i need a solution to find online users and way to send messages when users chat together, does any one write similar module like that?
Ashley Sheridan
2013-08-04 16:58:53 UTC
Permalink
Matijn Woudt <***@gmail.com> wrote:
>On Sun, Aug 4, 2013 at 4:00 PM, Ashley Sheridan
><***@ashleysheridan.co.uk>wrote:
>
>>
>>
>> Farzan Dalaee <***@gmail.com> wrote:
>> >
>> >
>> >> On Sun, 2013-08-04 at 14:56 +0430, Farzan Dalaee wrote:
>> >>>
>> >>> You mean when user logged in i add new record to table and when
>> >logged out i delete the row? So if user close the browser without
>> >logout how can i find user is online or not?
>> >>>
>> >>> Sent from my iPhone
>> >>>
>> >>> On Aug 4, 2013, at 14:44, Matijn Woudt <***@gmail.com> wrote:
>> >>>
>> >>> >
>> >>> >
>> >>> >
>> >>> > On Sun, Aug 4, 2013 at 11:02 AM, Farzan Dalaee
>> ><***@gmail.com> wrote:
>> >>> >> hi
>> >>> >> i want to write online user module for my site and i want to
>> >check
>> >>> >> $_SESSION['userID'] to find all users id who loged in
>> >>> >> but when i echo this code its return only current user detail
>> >>> >> how i can see all sessions?
>> >>> >>
>> >>> >> foreach($_SESSION as $k => $v)
>> >>> >> {
>> >>> >> echo $k."----------".$v;
>> >>> >> }
>> >>> >> or how i handle online users?
>> >>> >
>> >>> > You can only access sessions when you know the session id.
>> >>> > Most sites handle online users in their database, store a
>> >timestamp each time a user loads a page. When you want to display
>the
>> >online users, check where the timestamp is between now and a few
>> >minutes ago. Note that without javascript (or flash/java/etc) there
>is
>> >no way to truly know if the user left or not. The session will stay
>> >active for a long time, depending on your php.ini settings.
>> >>> >
>> >>> > - Matijn
>> >>> >
>> >>
>> >> Like Matijn said, unless you're using some kind of client-side
>method
>> >to continually poll the server, you can't know if they've just
>closed
>> >their browser. There are Javascript events for exiting a page, but
>they
>> >don't work correctly on Safari and iOS Safari.
>> >>
>> >> You don't have to actually save anything to the DB manually, just
>> >instruct PHP to use the DB for its own sessions, rather than files.
>> >>
>> >> Do you really need to inspect each visitors session in detail, or
>do
>> >you just need a way to determine how many unique visitors are on the
>> >site at any one time?
>> >>
>> >> Thanks,
>> >> Ash
>> >> http://www.ashleysheridan.co.uk
>> >>
>> >>
>> >>
>> >
>> >
>> >I need to inspect each visitor to show how online or who offline for
>> >chat
>> >Like facebook chat
>> >>
>>
>> Ah, so you don't need to see the details of the sessions then.
>Facebook
>> does this (badly) by using javascript on the client side which
>triggers an
>> update of a timestamp on the server, which then allows you to
>determine who
>> is online (or was within a given time limit)
>> Thanks,
>> Ash
>>
>
>Maybe it's bad, but there's no good alternative, except sending ping
>requests to your server every second or so, but any site as large as
>Facebook will DDOS itself when using things like that ;)
>
>- Matijn

I'm not saying the method is bad, but the way Facebook does it isn't great, I'm constantly seeing people online who "sign out" when I open up a message box. Now that might be genuine, but I'm not *that* unpopular!

Thanks,
Ash

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Paul M Foster
2013-08-05 03:24:22 UTC
Permalink
On Sun, Aug 04, 2013 at 05:58:53PM +0100, Ashley Sheridan wrote:

[snip]

>
> I'm not saying the method is bad, but the way Facebook does it isn't
> great, I'm constantly seeing people online who "sign out" when I open
> up a message box. Now that might be genuine, but I'm not *that*
> unpopular!

I'm sorry. Were you saying something? As soon as I saw "Ashley Sheridan"
in my email client, it closed and my computer rebooted for some strange
reason. ;-}

Paul

--
Paul M. Foster
http://noferblatz.com
http://quillandmouse.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Alessandro Pellizzari
2013-08-05 07:57:28 UTC
Permalink
Il Sun, 04 Aug 2013 20:47:37 +0430, Farzan Dalaee ha scritto:

Please use better quoting.

> So best way is use a script(javascript) to send ajax to server every 5
> second to check users is logged in or not? Is that okey?

It depends.

> I want to write chat module like facebook and i need a solution to find
> online users and way to send messages when users chat together, does any
> one write similar module like that?

Then knowing who is online is maybe the last of your problems.

You have to find a way to send the message to "user B" when "user A"
writes something.

You absolutely need javascript. You just need to find out how to connect
to the server. Have a look at socket.io, and find a php library that
supports it. I think it is the easiets way.

Be aware that the load on your server will be huge, growing exponentially
with the number of online users. You can't escape it, except by using
different technologies (XMPP as a protocol, with a javascript client, or
using node.js with socket.io, for example)

Bye.



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Tim Streater
2013-08-04 12:27:00 UTC
Permalink
On 04 Aug 2013 at 11:28, Ashley Sheridan <***@ashleysheridan.co.uk> wrote:

> Like Matijn said, unless you're using some kind of client-side method to
> continually poll the server, you can't know if they've just closed their
> browser. There are Javascript events for exiting a page, but they don't
> work correctly on Safari and iOS Safari.

onbeforeunload works fine in Safari; I use it all the time.

--
Cheers -- Tim
Ashley Sheridan
2013-08-04 12:36:29 UTC
Permalink
On Sun, 2013-08-04 at 13:27 +0100, Tim Streater wrote:

> On 04 Aug 2013 at 11:28, Ashley Sheridan <***@ashleysheridan.co.uk> wrote:
>
> > Like Matijn said, unless you're using some kind of client-side method to
> > continually poll the server, you can't know if they've just closed their
> > browser. There are Javascript events for exiting a page, but they don't
> > work correctly on Safari and iOS Safari.
>
> onbeforeunload works fine in Safari; I use it all the time.
>
> --
> Cheers -- Tim
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php


Apparently it has problems when browsing in "porn" mode, and mobile
safari has major problems with it. I'm basing this on previous posts on
forums I've been on, so it might have been fixed now, but I don't know
for sure either way.

Thanks,
Ash
http://www.ashleysheridan.co.uk
Loading...