0

I want to create a counter in PHP. Just a counter, nothing else. What's the best way to store the state of the counter?

EDIT 2:

Example 1(in pseudocode):

//Page is requested
obtain_lock();
$currentcounter=get_count();
$newcounter=$currentcounter+1
write_count($newcounter+1);
release_lock();
echo $newcounter;

Example 2(in pseudocode):

//Page is requested
atomic_inc_count();
echo get_count();

My question: what to use for get_count and write_count, or atomic_inc_count? A SQL DB? A file? What are the pro's and cons for using a file with locking vs a full-fledged database? What would you use?

EDIT 3: To clarify: I'm not actually looking for "thé approach", or one that is specifically tailored to my situation. I'm more hoping to see a couple of options with pro's and cons

Elon Bing
  • 83
  • 1
  • 6
  • Please explain your use case with more detail. It's hard to give a suggestion without knowing what the constraints are. – Lix Apr 03 '14 at 15:15
  • You could store it into a file, and then lock others from using it until that thread is finished ([`flock()`](http://us3.php.net/manual/en/function.flock.php)) – Chris Forrence Apr 03 '14 at 15:18
  • @Kermit: Thanks for taking the time to comment. I tried to rephrase and add as much information as I can reasonably add, but there's just not that much to it. The app literally **is** "just" a counter. – Elon Bing Apr 03 '14 at 15:29
  • 1
    @Taoelism Your question is still off-topic. A better question would be to take existing code and have a specific question about it. – Kermit Apr 03 '14 at 15:31
  • 594 **MILLION** results on google for [PHP Counter](https://www.google.com/search?q=php+counter) – Mike B Apr 03 '14 at 15:52
  • 3
    @Kermit: I find it hard to do that with this question, as there *is* no existing code, but I've added two possible solutions, in the hope that makes the answer clearer. I really appreciate your help(all of the commenters' help, by the way!). – Elon Bing Apr 03 '14 at 15:55
  • @MikeB I think Google is just being facetious – Kermit Apr 03 '14 at 15:57
  • @MikeB: I see your point; that's why Google was the first place I looked. None of the results on the first three pages of your query answer my question, however. I'm sure the answer is actually out there, but the general nature of the word "counter" makes it so that the information I'm looking for is hard - if not impossible to find. – Elon Bing Apr 03 '14 at 16:03
  • I think the problem is one of miscommunication. Judging by the comments, most StackOverflowers seem think I want to create a counter inside a larger application, to serve some purpose. This is not the case. My (finished) application is not going to exceed 5 LOC. My finished application is going to do exactly what I described: increment a counter, everytime the page is called. I think I'll call it "Counter". – Elon Bing Apr 03 '14 at 16:08
  • @Taoelism You are incorrect. Counters are a typical function of programmers who are learning. You're familiar with 'Hello World' programs? Counters are usually chapter 2 or used as an introduction to the filesystem. Here's an 8 line counter script http://www.phpjabbers.com/make-php-counter-php22.html. The next step would be to convert that script to use a database instead of flat files. Here's a 7 line counter using the DB http://stackoverflow.com/questions/20770544/hit-counter-with-php-and-mysql-database – Mike B Apr 03 '14 at 16:40
  • This addresses your question regarding db vs flat files http://stackoverflow.com/questions/2356851/database-vs-flat-files – Mike B Apr 03 '14 at 16:47
  • @MikeB: Neither of those links address any of my concerns. I do not want to run an entire database for just one variable, and I want to avoid the problem the phpjabbers article you linked brings forward, "A large number of file read/writes may corrupt the counter value". I know, on a technical level, how to implement a counter. What I'm looking for, is a correct, elegant approach. – Elon Bing Apr 03 '14 at 17:21
  • @Taoelism There is no correct approach to your specific situation without asking you a million questions (you need to pay for this kind of hand-holding). All we can do is expose you to the pros and cons of the tools that are out there. If you don't like the idea of running mysql just for a counter you can use sqlite instead. Doesn't require any installation on the server and is completely encapsulated by a single database file in your application. – Mike B Apr 03 '14 at 17:25
  • @MikeB: "All we can do is expose you to the pros and cons of the tools that are out there.". Yes! That is exactly what I want! I apologize for not formulating well enough or being clear enough. Could you... well... expose me to the pros and cons of the tools that are out there? Thank you for your patience and willingness to help so far, by the way. – Elon Bing Apr 03 '14 at 17:29

0 Answers0