How i know The current number of users online in my App ??

Got something cool to share with AppGini users? Feel free to post it here!
Post Reply
xbox2007
Veteran Member
Posts: 134
Joined: 2016-12-16 16:49

How i know The current number of users online in my App ??

Post by xbox2007 » 2020-04-07 11:02

hello

i would like to know The current number of users online in my App

i check in Admin area i dont fine any information .

i would like to add the current number of users online in my App on my App Home page ,

thanks a lot

User avatar
onoehring
AppGini Super Hero
AppGini Super Hero
Posts: 1158
Joined: 2019-05-21 22:42
Location: Germany
Contact:

Re: How i know The current number of users online in my App ??

Post by onoehring » 2020-04-09 07:09

Hi,

you will need to implement some criteria what "being online" means.
Is a user online with your app when he/she is logged in?
Is there an automatic logout when there is no action from the user?
What if a user decides to "stay logged in"? How should that be counted?
Can a user log in on different devices? How should that be counted?

Once you have made up your mind, you can write into a new table when the user logs in and logs out. Then it's simple - just count all log in, substract all log out and you should have an estimate of the actual logged in users.
I suggest that you create an automatic logout - and write that to the table. This logout, if not checked on a page-reload (if to much time has passed since last user action, log that user out), should probably use JS. You might know such automatic logout from your online bank which might show how much time is left - and make the logout after the time has passed autimatically.

Olaf

xbox2007
Veteran Member
Posts: 134
Joined: 2016-12-16 16:49

Re: How i know The current number of users online in my App ??

Post by xbox2007 » 2020-04-09 10:57

thanks for your replay

Is a user online with your app when he/she is logged in?

this one OK i have table for user when login with date and IP Address information

Is there an automatic logout when there is no action from the user?

I Dont have automatic logout information

What if a user decides to "stay logged in"? How should that be counted?

it will be count ( if there are four user already login to system ) will show number 4 User is online if one of them logout will show 3 User is online

Can a user log in on different devices? How should that be counted?

if login with different user name will add one

i dont know how i can find who logout ..

do you like help me to create an automatic logout information ???

thanks a lot

User avatar
onoehring
AppGini Super Hero
AppGini Super Hero
Posts: 1158
Joined: 2019-05-21 22:42
Location: Germany
Contact:

Re: How i know The current number of users online in my App ??

Post by onoehring » 2020-04-09 11:06

Hi

create a new file "loguserout.php" in your hooks directory with this code:

Code: Select all

<?
	$mi = getMemberInfo();  
	$ip = $_SERVER['REMOTE_ADDR'];
	$ts = date('Y-m-d H:i:s', strtotime("now"));
	$details = makeSafe($mi['username']) ." logged out.";
	sql("INSERT INTO yout_log_table_name_here set ipaddr='{$ip}', time_stmp='{$ts}', details='{$details}'", $eo);
	//END document login
?>
Change your /index.php (note: this file will be overwritten when you recreate your application with AG):
Search for

Code: Select all

if(isset($_GET['signOut'])) {
insert this after:

Code: Select all

include("{$currDir}/hooks/loguserout.php");
Done. IF the user clicks the logout button a record will be created.

Note 1: This does NOT cover automatic lopgging someone out.
Note 2: This does (probably) not cover different devices - you could take care of this in your SQL which you use to count people.

Olaf

xbox2007
Veteran Member
Posts: 134
Joined: 2016-12-16 16:49

Re: How i know The current number of users online in my App ??

Post by xbox2007 » 2020-04-09 12:11

thanks a lot

it work with me if i add all Code on Index.php

Code: Select all

<?php
	$currDir = dirname(__FILE__);
	define('HOMEPAGE', true);
	include("{$currDir}/defaultLang.php");
	include("{$currDir}/language.php");
	include("{$currDir}/lib.php");

	$x = new DataList;
	$x->TableTitle = $Translation['homepage'];

	$mi = getMemberInfo(); 	
	$ip = $_SERVER['REMOTE_ADDR'];
	$ts = date('Y-m-d H:i:s', strtotime("now"));
	$details = makeSafe($mi['username']) ." logged out";

	// according to provided GET parameters, either log out, show login form (possibly with a failed login message), or show homepage
	if(isset($_GET['signOut'])) {
		sql("INSERT INTO logout set ipaddr='{$ip}', ts='{$ts}', details='{$details}'", $eo);
		logOutUser();
		redirect("index.php?signIn=1");
	} elseif(isset($_GET['loginFailed']) || isset($_GET['signIn'])) {
		if(isset($_GET['loginFailed'])) @header('HTTP/1.0 403 Forbidden');
		include("{$currDir}/login.php");
	} else {
		include("{$currDir}/home.php");
	}
	
i create hooks/loguserout.php and add include("{$currDir}/hooks/loguserout.php"); in index.php but its not working

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1813
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: How i know The current number of users online in my App ??

Post by jsetzer » 2020-04-09 12:21

Just my five cents:

When user closes one of the open tabs or the browser itself there will not be any logout event, I'm afraid. Also if network is down. Also for session timeouts. Sometimes there may even be problems at midnight, for server reboots and/or twice a year at summertime/wintertime change. It is my experience from 30 years with different database-, webserver- and operating systems.

From my experience you should not rely on login/logout. It just does not work 100% in many situations.

You could use web socket technology but I would not recommend this.

Simpler approach:
You could use AJAX in the browser to periodically call a serverside php script which updates a last_active_on datetime field in your db. Then you can use sql to estimate the number of logged in users for example like this (pseudo sql)...

count distinct memberID
where last_active_on >= now()-10 minutes

This will always be an estimation.

For performance reasons I would execute that AJAX call on every page load + every 3 to 5 minutes. Definately not too often.

Hope this helps!

Best,
Jan
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

xbox2007
Veteran Member
Posts: 134
Joined: 2016-12-16 16:49

Re: How i know The current number of users online in my App ??

Post by xbox2007 » 2020-04-09 12:54

dear jsetzer thanks a lot

serverside.php its not AppGini script ,,,, i am dont have much information in MySQL , i just learn and try do simple things

i would like ask , ,,

if there are way to know How many Session is open in my App on current time like that i can know how many user are login ??

its also for security reason should know this information

in Admin Area i can find information for user and what user did ,,, but i cant see who is online !!!!


xbox2007
Veteran Member
Posts: 134
Joined: 2016-12-16 16:49

Re: How i know The current number of users online in my App ??

Post by xbox2007 » 2020-04-09 14:19

thanks a lot for your replay

i get simple idea and make it ,,,
its work but i am not sure its ok or not ( for security )

i create login table contain User name
will create new row for user when login

when user logout
i use

Code: Select all

 sql("DELETE from logs where User='{$User}'", $eo); 
in index,php
will delete row

the problem now how i can refresh page every 1 minute

User avatar
onoehring
AppGini Super Hero
AppGini Super Hero
Posts: 1158
Joined: 2019-05-21 22:42
Location: Germany
Contact:

Re: How i know The current number of users online in my App ??

Post by onoehring » 2020-04-09 14:43

Hi,

refresh page: Look here https://stackoverflow.com/questions/871 ... -meta-tags
Add

Code: Select all

 <meta http-equiv=”refresh” content=”60" />
to the header of your file (see hooks/tablename.php -> _init function).
Does it work?

The problem I see with your idea, is that if the user logs in twice, but logs out at once system - or simply does not log out, but closes the browser your table will not be clean.
Look into Jan's suggestion.

Olaf

xbox2007
Veteran Member
Posts: 134
Joined: 2016-12-16 16:49

Re: How i know The current number of users online in my App ??

Post by xbox2007 » 2020-04-09 15:03

The problem I see with your idea, is that if the user logs in twice, but logs out at once system - or simply does not log out, but closes the browser your table will not be clean.

that True , ,, i am still work around

thanks a lot for your information

Post Reply