Note that there are some explanatory texts on larger screens.

plurals
  1. POUser limit for my PHP app - with example code
    text
    copied!<p>I have a PHP app that I am looking to limit the number of users that can be active at any given time. I would like to keep it as light as possible as the reason for implementing it is because our current server can not handle the load - thus this is a temporary solution until we can upgrade. We store our sessions in memcache so I have written the following script to count the active sessions and store it in a flat file - this script is run every 5 mins.</p> <pre><code>&lt;?php $memcache = new Memcache; $memcache-&gt;connect('127.0.0.1', 11211); $activeSessions = $memcache-&gt;getStats(); $file_content = &lt;&lt;&lt;TEXT &lt;?php \$activeSessions = {$activeSessions['curr_items']}; ?&gt; TEXT; file_put_contents("activesessions.php", $file_content); ?&gt; </code></pre> <p>Now at the top of each page it is easy to find out how many users are currently active... my problem now is if there is more than the limit to stop more people creating sessions. The reason why this is a problem is I have no way of telling if the user loading the page already has an active session without doing something like:</p> <pre><code> &lt;?php include ('activesessions.php'); //uid is the users facebook id session_id(md5($uid.$secret)); session_start(); if ($activeSessions &gt; $limit &amp;&amp; empty($_SESSION)) { session_destroy(); include('limit_message.php'); exit; } ?&gt; </code></pre> <p>which should do the trick but in the process it will create a session to check if the session exists :S... thus skewing my active session count.</p> <p>So essentially I have a solution... my question though is does anyone have a better one? If however you think my solution is the best one please comment or up-vote to let me know :D</p> <p>Please help :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload