Just a quick idea: Instead of using a text file and some text file operations you could also use a Database (MySQL). There was a MySQL plugin around. Sorry that I don't have a direct link, check out the wiki and the user contributions forum if you want to use it.