Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
The best way to start is with my code:
PHP
$passwords = file_get_contents('87941192.php'); 
$passwords_array = array($passwords); 

if((array_key_exists($_POST['uname'], $passwords_array))  
        and  
    ($passwords_array[$_POST['uname']] == $_POST['pass'])){ 
      $_SESSION['user'] = $u; 
      $_SESSION['logged'] = "yes";
	  echo "Login Successful, You can now access your profile <br /> You can also make transactions now.";

      }else{ 

      print "username or pass was incorrect"; 
    
     } 

What happens is it gets passwords and usernames from a text file. Im not using a database as im only testing it, and there is no need at the moment. the passwords are in the format:

 'username' => 'password'
,'username1' => 'password1'

Every time it checks if the username and password are correct it turns back negative.

Why is this?
Posted

1 solution

This is because you don't even try to parse your data in file. The fact that the file is in PHP makes no sense at all. By reading your file, you don't interpret it as PHP code. Your call to file_get_contents returns only the string, nothing else:
http://php.net/manual/en/function.file-get-contents.php[^].

You need to parse each like (stop formatting it as a PHP file, it won't help you), read each line, parse it into key/value pairs (it depends on format you choose) and add data with keys to the associative array. All PHP arrays are associative arrays (ordered maps):
http://php.net/manual/en/language.types.array.php[^].

[EDIT]

If you want to find my first recommendation, you would need to read line by line. Please see some code samples here: http://stackoverflow.com/questions/6073235/how-to-read-line-by-line-in-php[^].

Alternatively, you can read all file in the array, but using the function file instead:
http://php.net/manual/en/function.file.php[^].

As documentation says (please see), each line in a file will correspond to an array element. But you need a different array, and array of passwords indexed with file username values.

Then you need to create a new array out of the line array, parsing each separate line into username/password pairs. You can use the method explode: http://php.net/manual/en/function.explode.php[^].



—SA
 
Share this answer
 
v2
Comments
G4mm4R4y 6-Aug-13 1:31am    
I've got the code online by stumbling across it, I figured I would give it a try, it's in php for I have no idea at all either but I figured it would not matter, how do I fix it excactly?
Sergey Alexandrovich Kryukov 6-Aug-13 11:38am    
This is not a productive approach. You need to write on your own. I explained you in detail what to do. Which part of it you don't understand?
—SA
G4mm4R4y 6-Aug-13 12:45pm    
Im confused: I want through line by line. I think it makes sense and should work. $passwords is the string of the passwords file. $passwords_array makes an array from the string. next it checks if the username(The key, is better to say) is in $passwords_array. It also checks if the username key is equal to the password value. But then it turns out false. Why is that? What am I explaining or getting wrong?
Sergey Alexandrovich Kryukov 6-Aug-13 13:37pm    
One string, only one string, can you understand it? Your $passwords_array is useless: it has only one element: all text.

If you read line by line, you have one line per string. So you could put a single line in a single array element. In PHP, array is an associative array, did you get it. It is indexed by some "keys". In particular, you don't need to fix the size of array, it grows as you add elements.

You are missing something very basic. What?

—SA
G4mm4R4y 6-Aug-13 13:45pm    
A string: A group of letters usually in a variable. I get it only one thing of text is basically what you are saying. I may have phrased that wrong. But only $passwords is a string. One string. A string the has this:
'username' => 'password'
,'username1' => 'password1'
Yes I get it. THAT variable is just a string. But the string is turned into an array:
$passwords_array = array($passwords);
So there we go its an array
Therefor $passwords_array is not useless, correct?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900