Questions:
- How can I get into the text file with PHP and break apart the username and password and then be able to match them together? What I have tried below isn’t working and I am not sure why since I am exploding it twice to separate them, but maybe I am not calling the variables correctly?
Text File Includes: daisy,password|**^**|donald,ducky|**^**|micky,mouse1|**^**|minnie,mousey
- The error code that checks to see if the username and password match and if not states ‘access denied’ is not triggering. I tried removing the
elseif
, but then everything was a ‘success’ even if nothing was entered.
<?php
//VARIABLES
$userError = $passError = $msg = $user = $pass = "";
$error = "";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
//PULL USERNAME AND PASSWORD FROM FILE THROUGH FUNCTION
$user = $_POST["user"];
$pass = $_POST["pass"];
// SUBMIT BUTTON HAS BEEN CLICKED
if(isset($_POST['submit']))
{
//OBTAIN USERNAME AND PASSWORD FROM FILE SEPARATELY
$passfile = fopen('includes/users.txt', 'r');
$contents = fread($passfile, filesize('includes/users.txt'));
$combos = explode('|**^**|', $contents);
foreach($combos as $combo)
{
$userInfo = explode(",", $combo);
//$user = $userInfo[0];
//$pass = $userInfo[1];
//if ($user == $_POST['user'] && $pass == $_POST['pass'])
if ($_POST['user'] == $userInfo[0] && $_POST['pass'] == $userInfo[1])
{
//$error = "";
$msg = 'Access Granted';
}
else
{
$error = true;
$msg = 'Access Denied';
}
}
}
//IF CLEAR HAS BEEN CLICKED
if(isset($_POST['clear']))
{
$user = "";
$pass = "";
}
}
?>