PHP – Concurrent User Control?

Question:

I have a finished and working system, made completely in ASP.

I'm rewriting version 2.0 of it, and as I'm going to review it 100%, I decided to try my luck with PHP. A problem has arisen in user control:

I need to have control of how many simultaneous users are using the system, as the monthly usage negotiations are linked to this.

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

I've looked into this but haven't found anything concrete on the best ways to do this in PHP, does anyone have any experience with something like this?

Answer:

Without database I would do like this:

<?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/pt_BR/function.session-save-path.php

Scroll to Top