Posts: 1,832
Location: Somewhere else entirely
|
Using a database requires alittle extra setting up (not much) and is much more reliable and easier once you get started. Using files would get messy very quickly. If you still want to try it, look at the filesystem functions.
A rough way to go about this would be to build an html form which submits the signup details, process the form to make the details fit in one line of text, write that to the file. When someone logs in you can search the file and compare the details:
register.php:
PHP Code:
<?php
function display_form() { ?>
<form action="<?= $_SERVER['PHP_SELF'] ?>" method="POST">
<input type="text" name="username" />
<input type="text" name="realname" />
<input type="password" name="password" />
<input type="submit" name="submit" value="Submit the Form" />
</form>
<? }
function process_form() {
$str = $_POST['username']. " " . $_POST['realname'] . md5($_POST['password']);
$fp = fopen("yourtextfile.txt","a"); // a is for 'append mode'
fwrite($fp,$str);
fclose($fp);
}
if( !isset($_POST['submit']) ) {
process_form();
}
else {
display_form();
}
login.php:
PHP Code:
<?php
function display_login() { ?>
<form action="<?= $_SERVER['PHP_SELF'] ?>" method="POST">
<input type="text" name="username" />
<input type="password" name="password" />
<input type="submit" name="submit" value="Login" />
</form>
<? }
function process_login() {
$lines = file("yourtextfile.txt");
$found = false;
foreach($lines as $value) {
$bits = explode(" ",$value);
$username = $bits[0];
if($username != $_POST['username']) continue;
$found = true;
$realname = $bits[1];
$hpassword = $bits[2];
if(md5($_POST['password']) == $hpassword)) {
echo "correct, log the guy in";
}
else {
echo "Incorrect password";
}
}
if(!$found) echo "Username not found";
}
if( !isset($_POST['submit']) ) {
process_login();
}
else {
display_login();
}
Important functions for file handling:
fopen(filename,access type)
access type is "r" for read "w" for write and "a" for append, they can also be combined. See the PHP reference for more. Returns a file pointer.
fwrite(file pointer, text)
Writes a line to a file. If in append mode, puts the new line at the end.
fclose(file pointer)
Closes the file once you are done - remember to do this.
$array = file(filename)
Reads an entire file into an array, with one line in each element
Also useful is:
explode(separator, text)
Splits up a string into an array. In this case I use it to split apart the pieces of info on one line in the file.
This example gives you a rough idea I hope. A proper script would have to do more checks, like to check that the username is not blank, or maybe a 'retype the password' box in the form to make sure it was typed ok.
EDIT: I haven't tested the above, could have made some silly mistake.
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';
Please login or register to view this content. Registration is FREE (aka MSN handwriting for forums)
Last edited by 0beron; 09-02-2005 at 06:32 PM..
|