Results 1 to 15 of 21
Thread: PHP Login Without MySQL
-
November 26th, 2007, 07:14 PM #1519Registered User
posts
PHP Login Without MySQL
Hello:
I have a situation where I need create a simple site login with PHP that doesn't rely on a database. So writing to a text file is the way to go (that file will be stored outside the document root). Security isn't a big concern though. I found this one sample:
http://www.webmaster-talk.com/php-fo...out-mysql.html
But that script has some problems and I haven't figured them all out (it's been a long while since I've used PHP). Can anyone help fix the syntax to get these files to work together?
-
November 26th, 2007, 07:26 PM #2
What errors are you getting mate?
-
November 26th, 2007, 07:38 PM #3519Registered User
postsIn register.php I'm getting an error on line 17 having to do with single quotes - not sure why:
Code:Parse error: parse error, unexpected '}' in c:\Apache\httpd\html\login_php\register.php on line 17
I found some missing braces earlier and added those. But obviously I need to move on from line 17 to get other error messages. The login.php page gives this error:Code:<?php if (isset ($_POST['submit'])) { $problem = FALSE; } if (empty ($_POST['username'])) { $problem = TRUE; print 'please enter a username!'; } if (empty ($_POST['password'])) { $problem = TRUE; print 'Please enter a password'; } if (empty ($_POST['password2'])) { $problem = TRUE; print 'Your password did not match your confirmed password'; } if ( !$problem ) { if ($fp = fopen ( 'users.txt', 'ab' ) { //Open the file! $dir = time() . rand 90, 4596); $data = $_POST['username'] . "\t" . crypt($_POST['password'] . "\t" . $dir . "\r\n"; // Write data and close file! fwrite ( $fp, $data ); fclose ( $fp ); mkdir ( $dir ) print 'You are now registered!'; } else { print 'You could not be registered due to an error'; } } else { print 'Please try again!'; } ?>
Code:Parse error: parse error, unexpected T_VARIABLE in c:\Apache\httpd\html\login_php\login.php on line 11
-
November 26th, 2007, 08:06 PM #4
Hm it seems like the print function is a bit weird. Try echo instead? And I'm not sure which lines they are on, since your source code has two linebreaks after each line. Not sure if that's intentional or not, but now I don't know which line 17 is - 17 or 34.
If you notice this notice you will notice that this notice is not worth noticing.
"Are you doing anything tonight? If not, how about me?"
Opera Sucks! - FIX IT
Oliver Zheng
-
November 26th, 2007, 08:15 PM #5
Yes this should work:
PHP Code:<?php
if (isset ($_POST['submit'])) {
$problem = FALSE;
}
if (empty ($_POST['username'])) {
$problem = TRUE;
echo 'please enter a username!';
}
if (empty ($_POST['password'])) {
$problem = TRUE;
echo 'Please enter a password';
}
if (empty ($_POST['password2'])) {
$problem = TRUE;
echo 'Your password did not match your confirmed password';
}
if ( !$problem ) {
if ($fp = fopen ( 'users.txt', 'ab' ) { //Open the file!
$dir = time() . rand 90, 4596);
$data = $_POST['username'] . "\t" . crypt($_POST['password'] . "\t" . $dir . "\r\n";
// Write data and close file!
fwrite ( $fp, $data );
fclose ( $fp );
mkdir ( $dir )
echo 'You are now registered!';
} else {
echo 'You could not be registered due to an error';
}
} else {
echo 'Please try again!';
}
?>
-
November 26th, 2007, 08:20 PM #6519Registered User
postsThanks - I'm getting an error now on Line 29 which is:
Which may just be write permissions to the text file, I'm not sure. Wait, it's a problem with the brace:Code:if ($fp = fopen ( 'users.txt', 'ab' ) { //Open the file!
Parse error: parse error, unexpected '{' in c:\Apache\httpd\html\login_php\register.php on line 29Last edited by Pherank; November 26th, 2007 at 08:22 PM.
-
November 26th, 2007, 08:27 PM #7
It's actually:
You forgot a parenthesis.PHP Code:if ($fp = fopen ( 'users.txt', 'ab' ) ) { //Open the file!
Fontaine Creative - Web Design Services
-
November 26th, 2007, 08:39 PM #8519Registered User
postsRight you are. But now the next line gets an error:
Parse error: parse error, unexpected T_LNUMBER in c:\Apache\httpd\html\login_php\register.php on line 31
There's obviously a closing parenthesis without an opening one - should that even be there?Code:$dir = time() . rand 90, 4596);
-
November 26th, 2007, 08:41 PM #9670Registered User
posts$dir = time() . rand (90, 4596);
try that?
-
November 26th, 2007, 08:49 PM #10519Registered User
postsMakes sense.

Next problem:
Parse error: parse error, unexpected ';' in c:\Apache\httpd\html\login_php\register.php on line 33
Why would the semi-colon be a problem?Code:$data = $_POST['username'] . "\t" . crypt($_POST['password'] . "\t" . $dir . "\r\n";
-
November 26th, 2007, 08:59 PM #11PHP Code:
$data = $_POST['username'] . "\t" . crypt($_POST['password']) . "\t" . $dir . "\r\n";
Fontaine Creative - Web Design Services
-
November 26th, 2007, 09:06 PM #12519Registered User
postsExcellent. That finishes register.php. Now for login.php (it's shorter):
Here's the first error:Code:<?php if (isset ($_POST['submit'])) { $loggedin = FALSE; $fp = fopen ( 'users.txt', 'rb' ); while ( $line = fgetcsv ($fp, 100, "\t")) { if ( ($line[0] == $_POST['username']) AND ($line[1] == crypt ($_POST['password'], l$line[1]) ) ) { $loggedin = TRUE; break; } } if ($loggedin) { print 'You are now logged in!'; } else { print 'The username and password did not match!'; ?>
Parse error: parse error, unexpected T_VARIABLE in c:\Apache\httpd\html\login_php\login.php on line 6
Code:if ( ($line[0] == $_POST['username']) AND ($line[1] == crypt ($_POST['password'], l$line[1]) ) ) {
-
November 26th, 2007, 09:10 PM #13519Registered User
postsOK, I see that one:
Code:l$lin
-
November 26th, 2007, 09:13 PM #14519Registered User
postsThe final error:
Parse error: parse error, unexpected $end in c:\Apache\httpd\html\login_php\login.php on line 16
That's the PHP closing tag, but I see no variable named $end in this script.
There's a brace missing at the end of the last print statement. That's all I see. Here's the full code for login.php:
<?php
if (isset ($_POST['submit'])) {
$loggedin = FALSE;
$fp = fopen ( 'users.txt', 'rb' );
while ( $line = fgetcsv ($fp, 100, "\t")) {
if ( ($line[0] == $_POST['username']) AND ($line[1] == crypt ($_POST['password'], $line[1]) ) ) {
$loggedin = TRUE;
break;
}
}
if ($loggedin) {
print 'You are now logged in!';
} else {
print 'The username and password did not match!';
}
?>Last edited by Pherank; November 26th, 2007 at 09:33 PM.
-
November 26th, 2007, 11:53 PM #15519Registered User
posts
I'm attaching the updated files. I think there must be a problem with the "while" statement in login.php, but I've had no further luck with this.
Basically, you create a user with the registration pages then you try to login with the login pages.

Reply With Quote



Bookmarks