User limit for my PHP application - sample code

I have a PHP application that I want to limit the number of users that can be active at any given time. I would like to keep it as light as possible, because the reason for its implementation is that our current server cannot handle the load - so this is a temporary solution until we can update it. We save our sessions in memcache, so I wrote the following script to count the active sessions and save it in a flat file - this script is run every 5 minutes.

<?php
  $memcache = new Memcache; 
  $memcache->connect('127.0.0.1', 11211);
  $activeSessions = $memcache->getStats(); 


$file_content = <<<TEXT
<?php

\$activeSessions = {$activeSessions['curr_items']};

?>
TEXT;

file_put_contents("activesessions.php", $file_content);
?>

, ... , , , , . , , , , , , , - :

    <?php

include ('activesessions.php');
//uid is the users facebook id
session_id(md5($uid.$secret));
session_start();
if ($activeSessions > $limit && empty($_SESSION))
{
   session_destroy();
   include('limit_message.php');
   exit;
}
?>

, , , : S... , .

, ... , - ? , , , , , : D

, :)

+5
1

session_id() - , sesssion , , PHP . , , - :

include ('activesessions.php');
//uid is the users facebook id

if ($activeSessions > $limit && session_id() == '')
{
   include('limit_message.php');
   exit;
}

session_id(md5($uid.$secret));
session_start();

, , cookie , .

+8

All Articles