Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Mar 2007
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    flat-file login system: cant find what wrong

    I have made some code for a flat-file login system, because my server doesnt have mysql, and I can see whats wrong, it registers fine, but when it comes to login... the code for common.php is
    PHP Code:
    <?php

    session_start
    ();

    function 
    registerUser($username,$password1,$password2){
        
    $errorText '';
        
    $validUser false;
        
        
    // Check the passwords
        
    if ($password1 != $password2$errorText "Your passwords dont match";
        elseif (
    strlen($password1) < 6$errorText "Your password is to short";
        
        
    // Check user existance    
        
    $pfile fopen("userpwd.txt","a+");
        
    rewind($pfile);

        while (!
    feof($pfile)) {
            
    $line fgets($pfile);
            
    $tmp explode('||'$line);
            if (
    $tmp[0] == $username) {
                
    $errorText "That user name is taken";
                break;
            }
        }
        
        
    // If everything is OK -> store user data
        
    if ($errorText == ''){
            
    // Encrypted password string
            
    $userpass md5($password1);
            
            
    fwrite($pfile"\r\n$username:$userpass");
        }
        
        
    fclose($pfile);
        
        
        return 
    $errorText;
    }

    function 
    loginUser($username,$password){
        
    $errorText '';
        
    $validUser false;
        
        
    // Check user existance    
        
    $pfile fopen("userpwd.txt","r");
        
    rewind($pfile);

        while (!
    feof($pfile)) {
            
    $line fgets($pfile);
            
    $tmp explode('||'$line);
            if (
    $tmp[0] == $username) {
                
    // User exists, check password
                
    if ($tmp[1] == md5($password)){
                    
    $validUser true;
                    
    $_SESSION['userName'] = $username;
                }
                break;
            }
        }
        
    fclose($pfile);

        if (
    $validUser != true$errorText "Invalid username or password";
        
        if (
    $validUser == true$_SESSION['validUser'] = true;
        else 
    $_SESSION['validUser'] = false;
        
        return 
    $errorText;    
    }

    function 
    logoutUser(){
        unset(
    $_SESSION['validUser']);
        unset(
    $_SESSION['userName']);
    }

    function 
    checkUser(){
        if ((!isset(
    $_SESSION['validUser'])) || ($_SESSION['validUser'] != true)){
            
    header('Location: login.php');
        }
    }

    ?>
    and heres my login
    PHP Code:
    <?php
    require_once('common.php');

    $error '0';

    if (isset(
    $_POST['submitBtn'])){
        
    // Get user input
        
    $username = isset($_POST['username']) ? $_POST['username'] : '';
        
    $password = isset($_POST['password']) ? $_POST['password'] : '';
            
        
    // Try to login the user
        
    $error loginUser($username,$password);
    }

    ?>
    my html code would go here
    <?php 
    }   
        if (isset(
    $_POST['submitBtn'])){

    ?>
    <?php
        
    if ($error == '') {
            echo 
    "Welcome $username! <br/>You are logged in!<br/><br/>";
            echo 
    '<a href="index.php">Now you can visit the homepage!</a>';
        }
        else echo 
    $error;

    ?>
            <br/><br/><br/>
    <?php            
        
    }
    ?>
    </body>

  • #2
    Regular Coder Armondo's Avatar
    Join Date
    Feb 2007
    Posts
    144
    Thanks
    3
    Thanked 0 Times in 0 Posts
    hmmm....idk ryan, maybe its like...well what are the errors your getting?
    ..
    ▲ ▲

  • #3
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,064
    Thanks
    2
    Thanked 317 Times in 309 Posts
    So, have you looked at the content of your userpwd.txt file and compared it with what your code is doing when it reads this file? Also, have you echoed any of the data to see what is being tested and used in the program to find where in the code it is working and at what point it is not?

    I see two problems in your code -

    1) You are rewinding the file pointer after you fopen() the file in the register and login code. When you open the file for append in your register code, this will move the file pointer from the end of the file to the beginning of the file. This will cause data at the start of the file to be overwritten.

    2) When you write to the file in the register code, you use a : as the separator. However, in all the other code you are exploding using ||, which is why I asked if you have looked at the content of your userpwd.txt file. The first step in troubleshooting is verifying that the data you are operating on is what you expect.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #4
    New Coder
    Join Date
    Mar 2007
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks soo much I was looking over and it was saving it as user : pass THEN it tried to explode it like it was user||pass thank you.


  •  

    Posting Permissions

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