Text File Username & Password

Questions:

  1. 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

  1. 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 = "";
		}
	}
	?>

Putting a username and plaintext password in a text file is the worst thing you could do. Don’t do that.

Yes, this is just for practice on learning how to pull from a file. I would never do this otherwise. :slight_smile:

This should get you going.

<?php

$contents = "daisy,password|**^**|donald,ducky|**^**|micky,mouse1|**^**|minnie,mousey";

$combos = explode('|**^**|', $contents);

foreach ($combos as $combo) {
    $userInfo = explode(",", $combo);

    if ($_POST['user'] == $userInfo[0] && $_POST['pass'] == $userInfo[1]) {
        // Username and password match
    }
}

@benanamen I updated my code and think I am close, but if I put in the first text file username and password it will say that access is denied and then replaces the user id in the form with the last username in the text file.

Ok, I will review your code and update it again if I am still missing the mark. Thank you!

@benanamen I updated the code (the code I updated it to before you sent that was fairly similar, yay!) and added in the information to pull from the text file. I also brought up my testing for matching the username and password.
I tested the code and it is only recognizing the last username/password in the file and not anything before that. They should always be [0] or [1] right so I am confused why it is only pulling the last pair as a success and nothing before it. Do I need another foreach loop for the username/password match?

Not sure what your doing. Here is an example matching the second pair.

<?php

$_POST['user'] = 'donald';
$_POST['pass'] = 'ducky';

$contents = "daisy,password|**^**|donald,ducky|**^**|micky,mouse1|**^**|minnie,mousey";

$combos = explode('|**^**|', $contents);

foreach ($combos as $combo) {
    $userInfo = explode(",", $combo);

    if ($_POST['user'] == $userInfo[0] && $_POST['pass'] == $userInfo[1]) {
       echo 'Match';
    }
}

I figured it out. I needed to add another line for the matching and do || for it. PHEW!

Big thank you for your help!

Sponsor our Newsletter | Privacy Policy | Terms of Service