0

How to validate a substring is true in PHP for example if user1 is in the string it should be true?

textfile:

user1 : pass1

user2 : pass2

user3 : pass3

if(in_array($_SERVER['user1'] . "\r\n", $textfile)){ //not the way want this to be true
  printf("Ok user1 is in this row somewhere");
}
3
  • Seriously, look at the manual. I don't mind beginner questions, but this code is so wrong I don't believe you put any effort in. Commented Apr 6, 2010 at 14:42
  • @coronatus any programming question is valid, and there is no php manual entry for authentication methods. Commented Apr 6, 2010 at 14:50
  • Why are you putting newlines after the username? I don't see them in the textfile. Can you do a print_r($textfile)? Also, is the username the key or value of $_SERVER['user1']? Commented Apr 6, 2010 at 14:54

3 Answers 3

1

I would advice against this kind of authentication system as is prone to errors or abuse. Use other system like ACL or database user/password hash check.

Sign up to request clarification or add additional context in comments.

Comments

1

As those above have said, this is not a good approach as far as user authentication goes. If you want something basic, look at using HTTP Authentication or something at least.

That said, you can do what you have asked using PHP's file function, e.g.

function validUser($file, $user, $pass) {
  // Check file exists
  if (!is_file($file)) {
    return FALSE;
  }

  // Read file
  $lines = file($file);
  if ($lines === FALSE) {
    return FALSE;
  }

  // Go over the lines and check each one
  foreach ($lines as $line) {
    list($fuser, $fpass) = explode(':', trim($line));
    if ($user == $fuser && $pass == $fpass) {
      return TRUE;
    }
  }

  // No user found
  return FALSE;
}

if (validUser('passwords.txt', 'foo', 'bar')) {
  echo 'The user was found';
}

Note that this assumes each line is of the form "username:password" with nothing else; you may need to adjust exactly how you match your lines depending on your format. An example file which would be validated by this would have a line such as

foo:bar

Comments

0

If you are using this for authentication; for the sake of your users consider a different (more secure) approach. By the way the reply to the OP is correct that just about nothing in that PHP code would work as appears to be intended.

However, if the idea is to use the value of an array by key $arr['key'] to look up configuration settings that need not be protected (for the world to see, basically) you can use the parse_ini_file() and friends. Again: this is not a good idea for truly sensitive data.

EDIT: Also, it is probably a good idea to use the PHP_EOL constant for end-of-line characters rather than assuming "\r\n".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.