PHP - Concurrent user control?

Asked

Viewed 805 times

6

I have a finished and working system, done in full ASP.

I am rewriting version 2.0 of it, and as I will review it 100%, I decided to try my luck with PHP. A problem arose in user control:

I need to be in control of how many concurrent users are using the system, because the negotiations of usage fees are linked to this.

In ASP the control was done through the ASP Application Object, which is a kind of global object accessible by all instances running the application (more information on it at this link).

I researched about it but I found nothing concrete about the best ways to do it in PHP, someone has some experience with something like?

  • As a curiosity, how do you identify with ASP the number of logged in users? You use Session_start to count the number of active sessions or something?

  • @Richarddias Basically this. Store in an Application a Sessionid (and some other information) of each login.

  • http://forum.imasters.com.br/topic/510280-contador-de-pessoas-online-em-tempo-real/

  • @Jeffersonsilva I came to see this topic already, but I wanted to try some solution without involving database as I already have working in ASP today.

1 answer

4


Without a database I would do so:

<?php
$tempoHoras = 6;
ini_set('session.gc_maxlifetime', $tempoHoras * 3600); # Tempo em segundos
ini_set('session.save_path', '/caminho/para/suas/sessoes'); # Local do salvamento

//inicia sessao
session_start();


function getUsuariosOnline()
{ 
    $count = 0; 
    $d = dir(session_save_path()) or die("Diretorio invalido.");
    while (false !== ($entry = $d->read())) 
    {
        if($entry != '.' && $entry != '..')
        {
            $count++; //Conta a qtde de arquivos dentro do diretorio de sessao
        }
    }
    $d->close();
    return $count;  
} 

$usuarios_online = getUsuariosOnline();
echo $usuarios_online;
?>

Ref: http://php.net/manual/en/function.session-save-path.php

  • The code worked smoothly, that’s kind of what I wanted... However, when testing it listed 8 users online (yes, I checked and there are really 8 files in the session directory). Can you tell why this happens? Thanks.

  • 2

    Probably your application has no session time set. That is, sessions are not expiring after X minutes of inactivity. You can set the lifetime of each session using http://php.net/manual/en/function.session-cache-expire.php

  • 1

    @Dirtyoldman Szag-Ot already edited the session to expire - change to the correct/desired value. The answer is complete. And Jefferson, session.cache_expire serves to expire the cache. It will not delete server sessions - just the cache. Have a look at session.gc_maxlifetime as edited by Szag-Ot in the original.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.