• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2272
  • Last Modified:

Need help with PHP flat file login system.

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/bt3software.com/public_html/LoginTest/validate.php on line 12
Could not open file /LoginTest/userlist.txt"

The code for the validate file around line 12 is:

"require_once('config.php');

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.  

-Jon

0
jon88654
Asked:
jon88654
  • 2
3 Solutions
 
Roger BaklundCommented:
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/bt3software.com/protected/userlist.txt" or a relative path: $file = "../../protected/userlist.txt"
0
 
Roger BaklundCommented:
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.

http://php.net/manual/en/function.md5.php
0
 
ciprian_dimofteCommented:
Your script is correct.
Just look in config.php

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

WITH:
$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.
Ciprian
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now