The forums have permanently moved to forum.kirupa.com. This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.


Page 1 of 2 12 LastLast
Results 1 to 15 of 21

Thread: PHP Login Without MySQL

  1. #1

    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?
    Attached Files Attached Files

  2. #2
    What errors are you getting mate?
    Web Design and Web Development blog
    Designer Notice: Browser List for design compatibility

  3. #3
    In 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
    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!';
    
    }
    
    ?>
    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:
    Parse error:  parse error, unexpected T_VARIABLE in c:\Apache\httpd\html\login_php\login.php on line 11

  4. #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

  5. #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 904596);

    $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!';

    }

    ?>
    Web Design and Web Development blog
    Designer Notice: Browser List for design compatibility

  6. #6
    Thanks - I'm getting an error now on Line 29 which is:

    Code:
    if ($fp = fopen ( 'users.txt', 'ab' ) {   //Open the file!
    Which may just be write permissions to the text file, I'm not sure. Wait, it's a problem with the brace:

    Parse error: parse error, unexpected '{' in c:\Apache\httpd\html\login_php\register.php on line 29
    Last edited by Pherank; November 26th, 2007 at 09:22 PM.

  7. #7
    It's actually:
    PHP Code:
    if ($fp fopen 'users.txt''ab' ) ) {   //Open the file! 
    You forgot a parenthesis.

  8. #8
    Right 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

    Code:
    $dir = time() . rand 90, 4596);
    There's obviously a closing parenthesis without an opening one - should that even be there?

  9. #9
    $dir = time() . rand (90, 4596);

    try that?

  10. #10
    Makes sense.

    Next problem:

    Parse error: parse error, unexpected ';' in c:\Apache\httpd\html\login_php\register.php on line 33

    Code:
    $data = $_POST['username'] . "\t" . crypt($_POST['password'] . "\t" . $dir . "\r\n";
    Why would the semi-colon be a problem?

  11. #11
    PHP Code:
    $data $_POST['username'] . "\t" crypt($_POST['password']) . "\t" $dir "\r\n"

  12. #12
    Excellent. That finishes register.php. Now for login.php (it's shorter):

    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!';
    ?>
    Here's the first error:

    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]) ) ) {

  13. #13
    OK, I see that one:
    Code:
    l$lin

  14. #14
    The 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 10:33 PM.

  15. #15

    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.
    Attached Files Attached Files

Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012