Jump to content

If statement with arrays


JordanStreet

Recommended Posts

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]
Link to comment
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
Link to comment
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.