Jump to content
Larry Ullman's Book Forums
Sign in to follow this  
bahaa

How To Get Online User And Online Guest ?

Recommended Posts

Hi all,

 

I have used the tutorial to store session in the database, but I am not quite sure how to use it to insert user id and also get online users and online gues.

In the book you show how to delete the session when some one log out. How to we delete the session when the user close the browser without login out?

 

Thanks in advance

Share this post


Link to post
Share on other sites

You can't delete the session when the user closes the browser without logging out (not without using JavaScript and Ajax, anyway). To see the active only users, check for sessions that have been modified within the past X number of minutes. This will not count people who left some time ago without logging out.

Share this post


Link to post
Share on other sites

I am using MySQL database and here is the session table

 

CREATE TABLE IF NOT EXISTS `session`(

 

sessionID CHAR(32) NOT NULL,

sessionData MEDIUMTEXT NOT NULL,

sessionTime TIMESTAMP NOT NULL,

sessionGuest TINYINT UNSIGNED NOT NULL ,

userID INT UNSIGNED COMMENT 'Fk to the user table',

 

CONSTRAINT pk_session_sessionID PRIMARY KEY(sessionID),

CONSTRAINT fk_user_session_userID FOREIGN KEY(userID) REFERENCES user(userID),

INDEX(sessionTime),

INDEX(sessionGuest)

 

 

)ENGINE = InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci;

 

Share this post


Link to post
Share on other sites

Okay, well, your constraints don't seem to make any sense and you shouldn't have sessionGuest and userID columns in the table. The session table just replaces storing sessions in a file. In any case, you would just do something like:

SELECT COUNT(sessionID) FROM session WHERE sessionTime > DATE_SUB(NOW(), INTERVAL 15 MINUTE)

That would count all the sessions where the sessionTime was modified in the past 15 minutes.

Share this post


Link to post
Share on other sites

What is the problem with the constraints?

Using the sql statement you provided, how would I get the online registered users and online guests? what should I do if I only want to get the online loggedin user in backened?

What If I want to get the name of the onlined logged in users?

 

If I want to know more about the strucuter of the whole database I would send it to you in private message.

Share this post


Link to post
Share on other sites

When you store session data in a database, the database table only uses three columns: the session ID, the session data, and a timestamp. The session data is not stored atomically as your layout suggests, it's all stored in the data column. Therefore, no constraints could be possible. I don't know where you got that database structure or how you're using it, but what you have is not designed for just having PHP store session data in a database.

 

If you understand how the sessions table works, you'd understand my SQL command. Only those with sessions would have a record in the sessions table. If there are 20 people using the site with sessions, then you'd have 20 records. If ten people log out, there are only 10 records. Eventually, through garbage collection, the old session records will get deleted automatically. My query therefore only selects records which have been modified within the past 15 minutes, which means that the user was active within the past 15 minutes (and logged in).

 

If you want to track guests separate from logged in, registered users, it would depend upon whether sessions are started for the non-logged-in users or not.

  • Upvote 1

Share this post


Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...
Sign in to follow this  

×
×
  • Create New...