Check if user exists in a text file

I am trying to create a simple create account page to use for a online page i am creating. This create account page asks the user to input a user name and a password to create an account. I then take the users inputs and store them in a txt file (I know this is not secure but this is what i am told to do for educational purposes) called accounts.txt in the following format:

user1,password1
user2,password2

So far i have the code to store in the text file however i need help with some error checking. I am able to check if the input fields are blank however i am not sure how to go about checking to see if a user name already exists in the text file to prevent have users with the same login name.
here is my php code

if (isset($_POST['submit_btn']))
      {
	$username = $_POST['name'];
  	$password = $_POST['pwd'];
	  if (empty($username))
	     {
		 echo "Please enter a username<br>";
	     } else $username = $username;
	  if (empty($password))
	     {
		 echo "Please enter a password<br>";
	     } else $password = $password;
	  $text = $username . "," . $password . "\
";
	  $fp = fopen('accounts.txt', 'a+');
	
	    if(!empty($username) && !empty($password) && fwrite($fp, $text))  {
        	header('Location: login.html');
    		}
         fclose ($fp);
     }

Well to answer your question specifically you will need to match the contents directly with using a preg_match or preg_match_all . But I’m highly against storing user information such as user credentials in a text file. It strait up stupid. please use sqlite if you need something small.

If you must use a file use md5 or sha1 hashing and then encrypt the file and separate each column with a separator and then you can use file_get_contents and then you can strtoken or explode to get the contents into an array and do all your operations on the array.

oh trust me i agree with you that it is stupid but since this is an assignment i have to follow the instructors guild lines.

Hi craft3maker, welcome to the forums

Can the text file be formatted as Comma Separated Variables (CSV)?

If so I think it would be better than using just a bunch of text.

I guess it depends on what you’re supposed to be learning, eg. file functions, regex, etc.

@Mittineague can you please clarify on how i can format them as a Comma Separated Variables(CSV). I have not gone over this topic yet so i am not sure if I could use it I was just told to store my user input as a text file in the format of
user1,password1
user2,password2
etc.

I have the text file in this format i just need to figure out how to check on the submission of the form to see if a username has already been used if so display a message like “user name is currently used try again”. Then redisplay the account creation page so the user can try again.

thanks for your help

Ok i figured out how to do it

<?php
if (isset($_POST['submit_btn'])) 
      {
	$username = $_POST['name'];
  	$password = $_POST['pwd'];
	  if (empty($username))
	     {
		echo "Please enter a username<br>";
	     } else $username = $username;

	  if (empty($password))
	     {
		 echo "Please enter a password<br>";
	     } else $password = $password;
	  $text = $username . "," . $password . "\
";
	  $fp = fopen('accounts.txt', 'a+');
	  $path = 'accounts.txt';
	  if (file_exists($path))
		{
 		$contents = file_get_contents($path);
 		$contents = explode("\
", $contents);
   		$users = array();
   		foreach ($contents as $value) {
      			$user = explode(',', $value);
      			$users[$user[0]] = $user[1];
    			}
    		if (isset($users[$_POST['name']])) {
	 		echo "that user already exists please enter a different user name";
    		}
		}
		else
		{
			echo "NO";
		}
	  if(!empty($username) && !empty($password) && fwrite($fp, $text) && !isset($users[$_POST['name']]))  {
        header('Location: login.html');
    	}
	  fclose ($fp);
$path = 'accounts.txt';
}
?>

now i just have to figure out how to implement this to work with my login page so the users can only access the page if they created an account.

Use $_SESSION :wink: