Need help with PHP flat file login system.

Posted on 2008-11-19
Last Modified: 2012-06-27
I am trying to create a flat file login system, when I log in to the system via the username and password I set in my .txt flat file, I recieve an error message that reads:

"Warning: file(/LoginTest/userlist.txt) [function.file]: failed to open stream: No such file or directory in /home2/shb8_008/ on line 12
Could not open file /LoginTest/userlist.txt"

The code for the validate file around line 12 is:


function checkLogin($user,$pass)
      global $file;      
      $users=file($file) or die("Could not open file <b>$file</b>");
      foreach($users as $userInfo)
            if ( trim(substr($userInfo,33))==$user && substr($userInfo,0,32)==$pass )
                  return 1;
      return 0;

I am new to PHP and would appreciate any help you guys gave provide.  Thanks a lot.  


Question by:jon88654
    LVL 39

    Accepted Solution

    The value of $file is wrong. Where is this file located? In the same directory as your script? Then use just $file="userlist.txt". This file should probably be in a directory where it is not readable by web users... Then you should use the full path to the file, i.e. $file = "/home2/shb8_008/" or a relative path: $file = "../../protected/userlist.txt"
    LVL 39

    Assisted Solution

    by:Roger Baklund
    You should also protect the passwords using MD5 or similar crypto hash.

    if ( trim(substr($userInfo,33))==$user && substr($userInfo,0,32)==md5($pass) )
                      return 1;

    Use the same function when you write the passwords to the userlist.txt file.

    md5() will create strings like this: 008c5926ca861023c1d2a36653fd88e2
    These strings are hard to decrypt, as md5 is a one-way function.
    LVL 1

    Assisted Solution

    Your script is correct.
    Just look in config.php

    $file = "/LoginTest/userlist.txt";

    $file = "LoginTest/userlist.txt";

    Make sure that the folder "Login Test" contains the file userlist.txt, otherwise you need to create it and add some users into it.

    Hope this helps.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Accessibility and Usability are two concepts that seem to be closely related.  But, too many people seem to have a distorted perception of them. During last five years, those two words have come to the day-to-day work of almost every web develope…
    Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
    This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
    Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now