Jump to content


Photo

If statement with arrays


  • Please log in to reply
7 replies to this topic

#1 JordanStreet

JordanStreet
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 12 August 2006 - 12:35 AM

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

#2 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 12 August 2006 - 12:39 AM

what does the file "members/$username" look like?  what are it's contents?

#3 mainewoods

mainewoods
  • Members
  • PipPipPip
  • Advanced Member
  • 685 posts
  • LocationMaine

Posted 12 August 2006 - 12:52 AM

you're missing a '$' at the beginning of this line:
info = file("members/$username");
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:
if($password == $info[0]){
are you storing information in files without file extensions. your code would implie that:
info = file("members/$username");
maybe add .txt?
$info = file("members/$username.txt");
check whether the file exists:
echo (file_exists("members/$username"))? "file exists" : "doesn't exist";


#4 JordanStreet

JordanStreet
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 12 August 2006 - 01:36 AM

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

#5 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 12 August 2006 - 01:38 AM

do a trim on $info[1].

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

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


#6 JordanStreet

JordanStreet
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 12 August 2006 - 01:42 AM

:) 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 ?

#7 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 12 August 2006 - 01:45 AM

It removes white space and newline characters...

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.


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

#8 JordanStreet

JordanStreet
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 12 August 2006 - 01:47 AM

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users