Ok, so basicly you want a text file to emulate a real database using simple php code... well you'll get many diferent answers for this one but I find that the following sample code is probably the easiest to implement and easiest to understand, probably not the fastest but it's a starting point. Now you could try simplifying this methood by creating a bunch of functions to perform the difernet tasks, but for simplicity sakes I'll just post in what needs to be posted for now.
Example 1: This code shows how to read in and display all the values in your database structure, notice the function fgetcsv() as it is the heart of the whole routine. It reads in the text file one line at a time and parses the line using the sting ":" as a separator.
<?php
//Show all entries in the database file
$fp = fopen("database.txt", "r+");
while (!feof($fp))
{
$data = fgetcsv ($fp, 4096, ":");
echo '<b>User:</b> '.$data[0].' <b>Pass:</b> '.$data[1].' <b>UID:</b> '.$data[2].'<br>';
}
fclose ($fp);
?>
Example 2: A simple expansion of this methood will allow you to read in all values in the database and then modify/display/save them at your leasiure.
<?php
//Read all data from the database file and parse into an array.
$fp = fopen("database.txt", "r+");
while (!feof($fp))
$data[] = fgetcsv ($fp, 4096, ":");
fclose ($fp);
//Print all values in the database.
for ($i=0; $i<count($data); $i++)
echo '<b>User:</b> '.$data[$i][0].' <b>Pass:</b> '.$data[$i][1].' <b>UID:</b> '.$data[$i][2].'<br>';
//Find user name 'user5' and modify password to = 'user5pass'
for ($i=0; $i<count($data); $i++)
{
if(!strcmp($data[$i][0], "user5"))
{
$data[$i][1] = 'user5pass';
break;
}
}
//Add a new user to the database
$userid = count($data)+1;
$password = "userpass".$userid; //Used for testing purposes,
$username = "user".$userid; //data would normaly come from post data from a form.
$data[$userid-1][0] = $username;
$data[$userid-1][1] = $password;
$data[$userid-1][2] = $userid;
//Save all values back to the database file.
$fp = fopen("database.txt", "w+");
$firstpass = TRUE;
for ($i=0; $i<count($data); $i++)
{
if ($firstpass) //Use $firstpass to prevent empty line at end of file.
{
$line = sprintf("%s:%s:%d",$data[$i][0],$data[$i][1],$data[$i][2]);
$firstpass = FALSE;
}
else
$line = sprintf("\n%s:%s:%d",$data[$i][0],$data[$i][1],$data[$i][2]);
fwrite($fp, $line);
}
fclose ($fp);
?>
I know these examples are kinda messy, but they are the best I can come up with off the top of my head here.
Now the one drawback of using a system like this is that this kind of management system can only be used by one person at any given time because the file data would loose any updates that might overlap each other... now you could try to either lock the file while it's in use or maybe use some of the other php functions to seek directly to the line you wish to modify, but thats more work, and as I said this is just the easy way I'm showing you right now. If you want to get into more complex stuff then we'll leave that for later. 🙂