|
You will be writing scripts that will require you to save information into some database, but you're hampered by a lack of MySQL. Some of my scripts use MySQL for storing info. Maybe your isp has php enabled, but does not provide mysql server. Well, all hope is not lost. In this tutorial, I'll show you how to write information to a simple text file, instead of a database.
First let's briefly go over security issues. MySQL allows you to create user accounts, and only such users can access the database. That provides security for your files. On the other hand, when you use a text file, like I'm about to show you, the file will just be a plain text file that can be accessed and viewed by anybody. What that means is that somehow, someone could see all the info on your users, which is not a good thing.
But what can be done to make the text file secure so nobody can access it, but still be available for reading and writing by your script? Well, two ways you can go about this:
OK, so now let's talk about writing to a flat text file instead of to a database. How difficult is it to understand what we're about to learn? It's very easy! You could learn the basics in less than 5 minutes. After that, spend time in exploring the complexities associated with it.
Reading and writing to a file is done by the php functions fopen( ), fputs( ), fread( ), fclose( ).
I can tell right away that the F that precedes each of these functions stands for the word file. Can you? :)
Let's look at them step by step.
$fp = fopen("filename","mode");
$fp = fopen("c:/windows/desktop/accounts.txt","a");
$str = "Hello World";
Keeping it simple, we're going to write "Hello World" into the file. $str will store that info. $str is just a variable to store the string we're going to write to accounts.txt.
$size = fputs($fp, $str);
$size stores the writing info, $fp we already know holds the file opening info, and $str holds the info to be written to the file.
Note: you don't have to create a file before hand before you can perform a read or write operation on it. If the file does not exist, php will automatically create it for you. Neato! In our example, if c:/windows/desktop/accounts.txt didn't exist, php creates it.
fclose($fp);
$filename = "c:/windows/desktop/accounts.txt";
$fp = fopen($filename,"a+");
$str = "Hello World";
$size = fputs($fp, $str);
fclose($fp);
?>
What other function could we have used to write to our file? We could have used fwrite() function. Both functions are identical in every way, and can be used interchangeably. Here's another neat example.
$filename = "c:/windows/desktop/test.txt";
$fp = fopen($filename,"w");
fwrite($fp, print (date ("l dS of F Y h:i:s A")));
fclose($fp);
echo "file was succefully written to.";
?>
The above should be clear enough to understand. First we open the accounts.txt file, then use fopen() function to open the file in "r" mode. We then read the contents of the file using fread() function. The content of the file is stored in the variable $contents. Finally we close the file.
When we use fread() to get the content of a file, how is the info stored in $contents? It is stored as one continous string from beginning to the end of the file.
But if the info is stored as one long string, how do you differentiate individual records? To answer this question, let's say you have login/password pairs stored in your file. Usually such pairs are stored in the format login:password. For example, we have three such pairs in our file
johnny:alps19
When we first read the info from the file, we get a long string like this
johnny:alps19 sinncity:pkmmjlo sachsd:9951648
But what we want is to separate the individual logins and passwords. This could be needed if you want to authenticate a user for example. The separation can be done easily. Php provides the explode() function.
$pairs = explode (" ", $contents);
$pairs = explode (":", $contents);
explode() requires two parameters: the delimiter (ie. something that separates the stuff we're trying to split apart), and the string to separate.
So in the example $pairs = explode (":", $contents);, our login:password pairs will be split up into two parts each. The result is stored in $pairs as an array. The string delimiter is :, and the string we're separating into parts is $contents, which we got from reading our file above. Of course, before we split the login/password pairs by "semicolon", we separated them by "space" first.
<body>
<form action="register.php3" method="post">
Choose a Login Name: <input type="text" name="login">
Choose a Password: <input type="text" name="passwd">
</form>
</body>
</html>
So let's say a user visits your site to register and types in richie as the login name, and am16fdmJ as the password. register.php3 will store the info as $login="richie", $passwd="am16fdmJ". Copy the form code above and paste it into notepad. Save it under your documents directory as register.html.
OK, so what will register.php3 look like? Let's take a look.
$filename = "c:/windows/desktop/user_accts.txt";
$fp = fopen($filename,"w");
$str = "$login" . ":" . "$passwd";
$size = fputs($fp, $str);
fclose($fp);
?>
Copy and paste the script into notepad. Save it to your web documents directory as register.php3. It must be in the same directory as register.html you saved earlier.
Let's take a look at the script. Most of it should be familiar to you already, especially the file-write part, which we tackled earlier on. We want to create a file called user_accts.txt, to be saved on the desktop. Php will automatically create the file if it doesn't already exist. user_accts.txt will contain login and passwords of your users or customers.
Take a look at the line $str = "$login" . ":" . "$passwd". Do you see anything peculiar about it? We have used two dots and a colon. A dot is used when we want php to concatinate (ie. join) strings. The colon will be the delimiter (separator) of our login/password pair.
That's all you need. What is left is for you to open your web browser, load the form register.html. Type in a username and password. Submit the form. A new file, user_accts.txt should be created on your desktop containing richie:am16fdmJ. If you're wondering if it's really this easy to create a user registration app on your site, don't be. It's really that simple!
So far I have shown you the basics of reading and writing to a flat file. I have also presented you with a real life example of how to use that knowledge to create an auto user registration on your site. The user registration script above is very basic. To use it on your site, there are things you should add to it. Things such as:
if(empty($login || $passwd)) { echo "error! Form must be filled in completely."; exit";}
if strcmp($passwd1 != $passwd2) { echo "Your passwords do not match!"; exit;}else{...
if substr($passwd,0,strlen($passwd) < 6) { echo "Your password must be atleast SIX characters long!"; exit;}else{...
mail($first." ".$last <$email>,"Dear $first,\n\nThanks for registering with our site!\n Here is your registration info\n Login: $login\n Password" $passwd...","From: www.YourSite.com
header (location: /somewebpage.html);
Most of the examples I have presented above are just some of the features you can introduce into your script to make it smarter. My next tutorial will address these issues, so you can create one hot User Registration script. See a complete user registration script here.
|