Flat file user database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
FlightFanatic
Forum Newbie
Posts: 11
Joined: Sun Feb 15, 2009 9:54 pm

Flat file user database

Post by FlightFanatic »

Hello, I am trying to create a flat file database, which stores the usernames and password in a file instead of a database. I am not sure how to do that. I tried putting it into a file, but it wouldne work checking if the usuername and password is correct... Any suggestions how to do this? Thanks!

-Flightfanatic
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: Flat file user database

Post by jayshields »

You might want to use something like SQLite to save yourself the hassle.
User avatar
JNettles
Forum Contributor
Posts: 228
Joined: Mon Oct 05, 2009 4:09 pm

Re: Flat file user database

Post by JNettles »

You're going to need to explain better what you're trying to do here. Precisely what are storing? How? What do you use to separate the data and individual records?

To tell the truth, you're only going to get so far into this before you're reinventing XML. Look into it. Ready-made parsers, lots of examples and free classes floating around.

Code: Select all

<user>
      <username>Dennis</username>
      <password>mrgoodbytes</password>
</user>
<user>
      <username>McLee</username>
      <password>shipyardbuilder</password>
</user>
FlightFanatic
Forum Newbie
Posts: 11
Joined: Sun Feb 15, 2009 9:54 pm

Re: Flat file user database

Post by FlightFanatic »

Hi Thanks for the replies!

@ jayshields, I tried to set up sqlite, but I couldnt figure it out... Do you have any suggestions how? Do I just have to upload it to my site directory?

@ I am just making a user register/login which stores the information into a file instead of a database, similar to navboard, or other file based scripts. It would most likely be storing a username and encrypted password in a file. Depending on how I could do this, I might take it further, and add more features. later.

Thanks,
-Flightfanatic
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: Flat file user database

Post by jayshields »

There's a lot of info on installing SQLite around, you can find it on their own site or through Google.
FlightFanatic
Forum Newbie
Posts: 11
Joined: Sun Feb 15, 2009 9:54 pm

Re: Flat file user database

Post by FlightFanatic »

Ok, I tried to make a registration page with php, which stors a username and password into a text file... that part works fine. I have that do it ok, but the part to check if the user is registering an already created username isnt working. Is there a way to prevent someone from registering the same username multiple times? Thanks

Here is my code so far:

Code: Select all

<?php
 
$user = $_POST['user'];
$pass = $_POST['pass'];
 
$userinfo = file("test.txt");
 
foreach($userinfo as $key => $val) 
{
   //explode that data into a new array:  
   $data[$key] = explode("||", $val);
}
 
for($k = 0; $k < sizeof($userinfo); $k++) 
{ 
echo '<tr><td>Name:</td><td>'.$data[$k][0].'</td></tr><br />';
echo '<tr><td>Email:</td><td>'.$data[$k][1].'</td></tr><br />';
}
//echo array_search("username=$user",$data,false);
if (in_array("username=$user",$data)){
echo "ERROR: Username already taken<br /> ". $data[$k][0];;
 
 
 
} else {
$test = fopen("test.txt","a");
 
if(!$test) {
    echo 'Error: Cannot open file.';
    exit;
}
 
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: Flat file user database

Post by jayshields »

What does your test.txt file look like?

You should really just use SQLite or XML or practically any other established flat-file data storage solution instead of trying to develop your own. If you intend to grow your website then rolling your own storage solution will get very complicated, very quickly.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Flat file user database

Post by AbraCadaver »

Assuming your file looks like (or similar):

Code: Select all

username||user@example.com||password
username2||user2@example.com||password
Then this should work just to check for the username:

Code: Select all

if(preg_match("/^$user\|\|/", implode($file))) {
    echo "ERROR: Username already taken<br /> $user";
}
You probably want to do the same with email:

Code: Select all

if(preg_match("/\|\|$email\|\|/", implode($file))) {
     echo "ERROR: Email address already registered<br /> $email";
}
Or if you don't need the $file array for later, then you can just do a file_get_contents() and skip the implode():

If you're going to go this route with your own storage, then you might start building a class or some functions, like: user_exists(), email_exists(), etc...
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
JNettles
Forum Contributor
Posts: 228
Joined: Mon Oct 05, 2009 4:09 pm

Re: Flat file user database

Post by JNettles »

If all you're doing is usernames/passwords and thats literally all you're checking just use XML with a structure like the one I gave you up above. Then you can just use SimpleXML to load it into an array without having to worry about writing your own parser.
Post Reply