SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Dec 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question 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
    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 "\n";
          
    $fp fopen('accounts.txt''a+');
        
            if(!empty(
    $username) && !empty($password) && fwrite($fp$text))  {
                
    header('Location: login.html');
                }
             
    fclose ($fp);
         } 

  2. #2
    SitePoint Zealot
    Join Date
    Jan 2011
    Location
    Portland
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.
    Last edited by jgetner; Dec 7, 2013 at 13:05. Reason: update to a better answer
    coming soon sitejuju.com my new development portfolio

  3. #3
    SitePoint Member
    Join Date
    Dec 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,162
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)
    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.

  5. #5
    SitePoint Member
    Join Date
    Dec 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @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

  6. #6
    SitePoint Member
    Join Date
    Dec 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok i figured out how to do it
    Code:
    <?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 . "\n";
    	  $fp = fopen('accounts.txt', 'a+');
    	  $path = 'accounts.txt';
    	  if (file_exists($path))
    		{
     		$contents = file_get_contents($path);
     		$contents = explode("\n", $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';
    }
    ?>

  7. #7
    SitePoint Member
    Join Date
    Dec 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  8. #8
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by craft3maker View Post
    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
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •