Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>What you are doing is a very bad idea</strong></p> <p>But lets first answer the actual question you are asking.</p> <p>To be able to do that you will have to actually process the file first in some kind of data structure that allows for that to be done. I'd presonally recommend an array in the form of <code>IP =&gt; AMOUNT</code>.</p> <p>For example (untested code):</p> <pre><code>$fd = file($file); $ip_list = array(); for ($fd as $line) { list($ip, $amount) = explode("-", $line); $ip_list[$ip] = $amount; } </code></pre> <p>Note that the code is not perfect as it would leave a space at the end of <code>$ip</code> and another in front of <code>$amount</code> due to the nature of your original data. But it works good enough just to point you in the right direction. A more "accurate" solution would involve regular expressions or modifying the original data source to a more convenient format.</p> <hr> <p><strong>Now the <em>real</em> answer to your actual problem</strong> </p> <p>Your process will quickly become a performance bottleneck as you would have to open up that file, process it and write it all back again afterwards (not sure if you can do in-line editing of an open file) <strong>for every request</strong>.</p> <p>As you are trying to do some kind of per-IP hit count, there are a lot of better solutions to your problem:</p> <ul> <li>Use an existing solution for it (like <a href="http://piwik.org/" rel="nofollow">piwik</a>)</li> <li>Use an actual database for your data</li> <li>Keep your file simple with just a list of IPs and post-process it off-line periodically to make it be the format you want</li> <li>You can avoid writing that file altogether if you have access to your webserver's logs (and they are setup to log every request with the originating IP) and you can post-process <em>that</em> file instead</li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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