Jump to content

Archived

This topic is now archived and is closed to further replies.

JordanStreet

If statement with arrays

Recommended Posts

here is my script

$password = 'mypass';

info = file("members/$username");

if($password == $info[1]){
  echo "login succes";
  }else{
  echo "wrong password";
  }

however no matter what it always returns false

Share this post


Link to post
Share on other sites
you're missing a '$' at the beginning of this line:
[code]info = file("members/$username");[/code]
don't know where you stored your password, but the first line from the file will be the '0' element (not '1') of the array:
[code]if($password == $info[0]){[/code]
are you storing information in files without file extensions. your code would implie that:
[code]info = file("members/$username");[/code]
maybe add .txt?
[code]$info = file("members/$username.txt");[/code]
check whether the file exists:
[code]echo (file_exists("members/$username"))? "file exists" : "doesn't exist";[/code]

Share this post


Link to post
Share on other sites
okay here is the full code

<form action="login.php" method="post">
Username:<br/><input type="text" name="username"><br/>
Password:<br/><input type="password" name="password"><br/>
<input type="submit" name="submit" value="Login">
</form>

<?

if(isset($_POST['submit'])){
 
$username = $_POST['username'];
$password = $_POST['password'];

if(file_exists("members/$username")){

$info = file("members/$username");

if($password == $info[1]){
  echo "login succes";
  }else{
  echo "wrong password";
  }

}else{
  echo "Username not found please try again.";
}

}

?>

and when i submit the form i use Jordan as the username and in the file members/jordan (no file extension) I have my password stored on the second line


the thing is i can echo $info[1] I just cant use it in the if statement

hope this clears things up

Share this post


Link to post
Share on other sites
do a trim on $info[1].

You need to remove the newline character from the end, otherwise they won't match.

[code]f($password == trim($info[1])){
  echo "login succes";
  }else{
  echo "wrong password";
  }[/code]

Share this post


Link to post
Share on other sites
:) worked like a charm.  Thanks a bunch man.  However im looking in the php manual about the trim function and im a lil confused because as far as I know there was no what space in my text file.  Could you maybe explain this a little more ?

Share this post


Link to post
Share on other sites
It removes white space and newline characters...

[quote] This function returns a string with whitespace stripped from the beginning and end of str. Without the second parameter, trim() will strip these characters:

    *

      " " (ASCII 32 (0x20)), an ordinary space.
    *

      "\t" (ASCII 9 (0x09)), a tab.
    *

      "\n" (ASCII 10 (0x0A)), a new line (line feed).
    *

      "\r" (ASCII 13 (0x0D)), a carriage return.
    *

      "\0" (ASCII 0 (0x00)), the NUL-byte.
    *

      "\x0B" (ASCII 11 (0x0B)), a vertical tab. [/quote]

The one's that pertain to your situation are the \n and \r

Share this post


Link to post
Share on other sites
yeah i echoed them again and noticed that there is a white space after the non trimed one.  Yeah this deff brings some insight.  Thanks again  ;D

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.