Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Check if user is already logged in

15 views
Skip to first unread message

fr?

unread,
Oct 7, 2003, 3:36:21 PM10/7/03
to
Hi,

i have a website , on wich users have to log in
credentials are checked against mysql db
some session vars are set during login for use somewhere else in the
code.

Is there a way to prohibit a user to log in twice.
A was thinking about setting some flag in the db, but in that case i
need some timeout in case a pc can crash for instance so the user does
not logout.

Is it possible to check if a session var ( in another session )
exists??

thanks
fré

Paulus Magnus

unread,
Oct 7, 2003, 8:52:25 PM10/7/03
to
"fr?" <gri...@chello.nl> wrote in message
news:fd172af0.03100...@posting.google.com...

Session variables are accessible throughout a site, do a print_r
($_SESSION); to see what's in there. Maybe your application isn't using
PHP's sessions but some internal mechanism for dealing with them.

Paulus


griemer

unread,
Oct 8, 2003, 1:38:08 PM10/8/03
to
as expected, i only see the sessionvars belonging to my own session
On PC1 script says: Array ( [id] => 11 [user] => john )
On PC2 script says: Array ( [id] => 34 [user] => mark )

I cannot see the vars for PC1 on PC2 v.v.. That is probably how it is
meant to be.

What i am trying to check is:
john is logged into PC1
Someone else tries to login on PC2 with username John. Is there a way to
find out that john is already logged in, and prevent this user to login as
John??

fré


"Paulus Magnus" <paulus...@loves-spam.com> schreef in bericht
news:blvn48$5c3$1...@titan.btinternet.com...

Paulus Magnus

unread,
Oct 8, 2003, 1:52:33 PM10/8/03
to
"griemer" <gri...@chello.nl> wrote in message
news:4YXgb.34297$4w.42...@amsnews03.chello.com...

> as expected, i only see the sessionvars belonging to my own session
> On PC1 script says: Array ( [id] => 11 [user] => john )
> On PC2 script says: Array ( [id] => 34 [user] => mark )
>
> I cannot see the vars for PC1 on PC2 v.v.. That is probably how it is
> meant to be.
>
> What i am trying to check is:
> john is logged into PC1
> Someone else tries to login on PC2 with username John. Is there a way to
> find out that john is already logged in, and prevent this user to login
as
> John??

Ah I see what your problem is now. Sessions are related to one browsing
session only so no you couldn't see other sessions that were active without
directly accessing the session files on the server, which isn't a wise thing
to do.

With a web browser you can't see when somebody is logged in, only the last
time they did something. If John simply turned his machine off the session
on the server will remain there ad infinitum. You could log the user ids to
a database during login and then you can detect during login that he's
already logged in but he may have rebooted his computer and be trying to get
back in instantly so you'd have to take account of that. Maybe popping up an
Alert box that says "John was active on PC1 45 seconds ago, do you want to
log him out and login as John on this PC?" or something similar. Your only
opportunity to interact with the server is when a page is requested. Sitting
and reading a page, having a coffee, going to the toilet or turning off your
computer don't trigger data transmissions so they're invisible to a web
server.

Paulus


R. Rajesh Jeba Anbiah

unread,
Oct 13, 2003, 8:36:39 AM10/13/03
to
gri...@chello.nl (fr?) wrote in message news:<fd172af0.03100...@posting.google.com>...

> Hi,
>
> i have a website , on wich users have to log in
> credentials are checked against mysql db
> some session vars are set during login for use somewhere else in the
> code.
>
> Is there a way to prohibit a user to log in twice.

Yes, Store both session_id & IP in table, and check the validity
of session on each pages. You may look at
http://martin.f2o.org/download/php-login-script

> A was thinking about setting some flag in the db, but in that case i
> need some timeout in case a pc can crash for instance so the user does
> not logout.
>
> Is it possible to check if a session var ( in another session )
> exists??

/**
* IsSessionActive()
*
* @author R. Rajesh Jeba Anbiah
* @description Will work only if the session save handler is default
"files" implementation & session gc probability is higher
* @param $session_id
* @return true/false
**/
function IsSessionActive($session_id)
{
$sess_file = session_save_path()."/sess_".$session_id;
return(file_exists($sess_file));
}


---
"US got a nuclear bomb that can destroy the world 13 times. Russia
got a nuclear bomb that can destroy the world 7 times. But...my
friend! Tell me! CAN YOU KILL A MAN TWICE??!!!!!" -- P.A.Sangma, Peace
loving Indian politician against India's step to go for a nuclear
test.
Email: rrjanbiah-at-Y!com

Kevin Thorpe

unread,
Oct 13, 2003, 9:02:02 AM10/13/03
to
R. Rajesh Jeba Anbiah wrote:

> gri...@chello.nl (fr?) wrote in message news:<fd172af0.03100...@posting.google.com>...
>
>>Hi,
>>
>>i have a website , on wich users have to log in
>>credentials are checked against mysql db
>>some session vars are set during login for use somewhere else in the
>>code.
>>
>>Is there a way to prohibit a user to log in twice.
>
>
> Yes, Store both session_id & IP in table, and check the validity
> of session on each pages. You may look at
> http://martin.f2o.org/download/php-login-script

Unfortunately this might not always work. We have some customers on AOL
or behind university round-robin proxies. The requests from these users
come from different IP addresses for each page. If you look in the
request headers
(http://www.zend.com/manual/function.apache-request-headers.php)
I think most proxies add an X-forwarded-for: x.x.x.x header with the
real IP address.


lonegunmen

unread,
Oct 13, 2003, 4:36:47 PM10/13/03
to
Kevin Thorpe <ke...@pricetrak.com> wrote in message news:<3f8aa25d$0$11458$afc3...@news.easynet.co.uk>...


I and another prgrammer recently faced a similiar problem. He ran the
java side of our site, and I ran the php side. We had to find a way
to have users log in once on either side and have that login remain
valid for the other side etc etc. Timeouts were a necessity for us
also. In short we set up a sessions table in our db which we would
write our own sessions into while also placing an MD5 sum in a cookie
on the client computer that contained username, pass and date I
believe. It was a while ago. We would then make a call to that
sessions table at page changes which allowed logins to remain valid
accross the language change as long as the user had a valid session.
That's probably a bit more than you'll need to do given that you're
just working in php, but thought I'd post it.

As far as the time out thing, I found it easier to make the login
script simply overwrite any existing sessions if there was a name
duplication. This took care of the comp crashing issues. In
addition, we are running a cronjob every fifteen minutes that queries
the db using a two hour interval to check for stale sessions. To make
that effective, we wrote an automatic session update into our session
checking script etc etc. Just a timestamp update. Have been running
this set up for several months now and it has been working pretty
well.

R. Rajesh Jeba Anbiah

unread,
Oct 15, 2003, 12:37:36 AM10/15/03
to
Kevin Thorpe <ke...@pricetrak.com> wrote in message news:<3f8aa25d$0$11458$afc3...@news.easynet.co.uk>...
> R. Rajesh Jeba Anbiah wrote:
>
> > gri...@chello.nl (fr?) wrote in message news:<fd172af0.03100...@posting.google.com>...
> >
> >>Hi,
> >>
> >>i have a website , on wich users have to log in
> >>credentials are checked against mysql db
> >>some session vars are set during login for use somewhere else in the
> >>code.
> >>
> >>Is there a way to prohibit a user to log in twice.
> >
> >
> > Yes, Store both session_id & IP in table, and check the validity
> > of session on each pages. You may look at
> > http://martin.f2o.org/download/php-login-script

Storing session_id alone is quite sufficient. When the user login
to the webpage, store the current session_id in DB and then check the
current session_id on each page with the one in DB. IP check is to
enhance with the error message like "You have been logged off as you
have logged in on a different machine."

> Unfortunately this might not always work. We have some customers on AOL
> or behind university round-robin proxies. The requests from these users
> come from different IP addresses for each page. If you look in the
> request headers
> (http://www.zend.com/manual/function.apache-request-headers.php)
> I think most proxies add an X-forwarded-for: x.x.x.x header with the
> real IP address.

Could you post your IP fetch routine? I guess, the problem is
in your code.

---
"We are free from today... Paralyse the country, you are your own
leaders. Do or Die." --- Mahatma Gandhi
Email: rrjanbiah-at-Y!com

0 new messages