I know this isn't very secure but it doesn't need to be for my application, but I am storing usernames, passwords, and the users authorized cameras in a txt file and then when a user logs in searching the txt file to determine if there is a match, retrieving the information if it is found and displaying an error message if it is not.
Each user entry is stored in this format:
user:password
cam0,cam1,cam2,cam3
The problem I am running into is that if I manually edit the txt file it will find the user and authenticate no problem, but if I use the add user code it adds the user information to the txt file correctly, looks just as if I had typed it in manually, but it will not authenticate unless I go back in and manually retype that users information. I'm thinking maybe it's whitespace somewhere but I am using trim to try to eliminate that. When debugging the $entry array contains all the correct information but the $key variable has a null value.
I would greatly appreciate any help anyone could offer. Thanks!
Here is my code:
Authentication Page:
CODE
session_start();
//Retrieve user entry
$userid = trim($_POST['userid']);
$password = trim($_POST['password']);
$authentication = $userid.':'.$password;
$filename = "fields.txt";
$handle = fopen($filename, "rb");
$contents = fread($handle, filesize($filename));
$entry = explode("\n", $contents);
//Authenticate login against fields.txt
$key = array_search($authentication, $entry);
if (is_numeric($key)==false){
//Debug
echo $entry[0];
echo $entry[1];
echo $entry[2];
echo $entry[3];
echo $key;
echo '<br>';
echo $authentication;
echo '<br>';
echo $userid;
echo '<br>';
echo $password;
Add User Page:
CODE
session_start();
//Define Variables
$filename = 'fields.txt';
$fp = fopen($filename, 'a');
$userid = trim($_POST['newuserid']);
$password = trim($_POST['newpassword']);
$cam0 = trim($_POST['cam0']);
$cam1 = trim($_POST['cam1']);
$cam2 = trim($_POST['cam2']);
$cam3 = trim($_POST['cam3']);
$array[0] = "Camera 1";
$array[1] = "Camera 2";
$array[2] = "Camera 3";
$array[3] = "Camera 4";
$array[4] = "Camera 5";
$array[5] = "Camera 6";
$array[6] = "Camera 7";
$array[7] = "Camera 8";
$array[8] = "Camera 9";
$array[9] = "Camera 10";
$array[10] = "Camera 11";
$array[11] = "Camera 12";
$array[12] = "Camera 13";
$array[13] = "Camera 14";
$array[14] = "Camera 15";
$array[15] = "Camera 16";
$cam0 = array_search($cam0, $array);
$cam1 = array_search($cam1, $array);
$cam2 = array_search($cam2, $array);
$cam3 = array_search($cam3, $array);
//Define data to write to file
$line0 = "\r\n";
$line1 = trim($userid);
$line2 = ":";
$line3 = trim($password);
$line4 = "\r\n";
$line5 = trim($cam0);
$line6 = ",";
$line7 = trim($cam1);
$line8 = ",";
$line9 = trim($cam2);
$line10 = ",";
$line11 = trim($cam3);
//Write user data to file
if (is_writable($filename)) {
fwrite ($fp, $line0);
fwrite ($fp, $line1);
fwrite ($fp, $line2);
fwrite ($fp, $line3);
fwrite ($fp, $line4);
fwrite ($fp, $line5);
fwrite ($fp, $line6);
fwrite ($fp, $line7);
fwrite ($fp, $line8);
fwrite ($fp, $line9);
fwrite ($fp, $line10);
fwrite ($fp, $line11);
fclose($fp);