2

Well, the title explained a lot. I'm about to make 1 file object (let's called analytic.json), which is contains object like :

{"country":{"indonesia": 
    [{"uniqueID":'00000000000',"dateVisit":"20170129","visitTimes":17}, 
     {"uniqueID":'00000000001',"dateVisit":"20170129","visitTimes":10}]}}

In this case, I'd like to track visitors. Each visitor known as 1 uniqueID, and each time visitor open a new page the visitTimes will be raised. The problem I'm concerning about is when it's about > 1000 visitors in one time open any page together, what will be happen?

Each open and rewrite to this file is done with fopen, fwrite, and fclose each time its done. Any way doing this better without using database? Do I need to make a function to queuing those process? And how? A lot question, really appreciate that.

hansaplast
  • 11,007
  • 2
  • 61
  • 75
Rey Musa
  • 122
  • 6
  • 2
    Since you did mention the least useful file IO functions for that, but omitted the note about locking; no, this is not a workable approach. What's the exact qualm with the better approach that you already know about? – mario Jan 17 '17 at 18:44
  • You would need to use file locking, but we can only discourage such attempts. A file based storage simply is the wrong approach. A database is the way to go. – arkascha Jan 17 '17 at 18:46
  • 1
    To add to the dogpile here, the file locking is only one problem. What happens when your file is 4-5GB? – Machavity Jan 17 '17 at 18:54

1 Answers1

2

First of all as already mentioned in the comments you DEFINITLY should use a database for this purpose. This is what databases are built for and if you use a file as a database the following problems will occur:

  1. Needs more time (open and close)
  2. Needs more space
  3. The file lock will slow down your server (and it will be required)
  4. The file will grow fast and therefore the more visitors you have the more your server will be overloaded - no chance as soon as you have a significant amount of visitors

Read the following discussion on simultaneous requests in PHP: Simultaneous Requests to PHP Script

Instead of using your file solution (which is a bad idea) make a nice database concept. You can update, query, lock and also make a nice function to deliver the current state as JSON.

Community
  • 1
  • 1
Blackbam
  • 17,496
  • 26
  • 97
  • 150