Page 1 of 1

Text file database

Posted: Fri Jan 08, 2010 8:19 pm
by Basic
Hi.

I need help constructing a text file database.
The database is going to look something like this:

10.0.0.1#1#10#1.0#1263003237
10.0.0.2#0#3#1.0#1263003237
50.283.192.19#0#0#1.0#1263003237

I think you get my point ;)

Anyways, lets say I got a file called server.php
Then for example someone access it with the post server.php?ip=10.0.0.1&pw=1&pl=8&v=1.0
Now the script should go into our database (lets call it serverlist.txt).
First it should check each row to see if the last variable (timestamp) haven't surpassed etc. 10 min, otherwise it should remove the current row.
Next it should check if the ip variable it's providing, already exists in the db, and if so it should update it, otherwise it should just create a new.

So it's basically a server list based on a txt file and a php script to update it :)

I don't have that much knowledge in PHP, so if anyone is neat with it and could make me at least some of the code, I would be really happy :)

Thanks alot!

Re: Text file database

Posted: Fri Jan 08, 2010 8:55 pm
by requinix
The only reason I haven't just given you a list of functions and an explanation of how to piece them together is because I haven't coded PHP in a while and I miss it.

Code: Select all

function addServer($filename, $ip, $pw, $pl, $v) {
    // this function can only be running one at a time over the ENTIRE SERVER
    $h = fopen($filename, "r+t"); if (!$h) return false;
    if (!flock($h, LOCK_EX)) return false;
 
    $servers = array();
    while (!feof($h)) {
        // read a line
        $line = fgetcsv($h, 1024, "#"); if (!$line || count($line) != 5) continue;
        list($lip, $lpw, $lpl, $lv, $lt) = $line;
 
        // timestamp
        if ($lt + 600 < time()) continue;
 
        // already exists
        if ($lip == $ip) continue;
 
        // save
        $servers[] = $line;
    }
    $servers[] = array($ip, $pw, $pl, $v, time());
 
    // rewind and write servers
    rewind($h);
    foreach ($servers as $line) fputcsv($h, $line, "#", "");
    ftruncate($h, ftell($h)); // cut off any extra
 
    fclose($h);
}
Untested. Because it wouldn't be fair for me to do everything.