I have made a program that lets you enter the username and password then it stores it in a text file and when i want to login i want to make it so it loops through the text file that has the usr/pass in to find if you entered your credentials in correctly. I'm not sure how to do this. please can somebody help
2 Answers
Example text.txt file:
$ cat text.txt
text1|answer1 text2|answer2 text3|answer3
Example code:
cat test2.php
<?php
$text="text1";
$file="text.txt";
$f = fopen($file, 'r');
while($data = fgets($f))
{
$ar_data=explode('|',$data);
if($ar_data[0]==$text) {
echo "looking for: ".$ar_data[1]."\n";
}
}
Example usage:
$ php test2.php
looking for: answer1
The text fileis not a good way for verify user credentials. You should try sql database. sqlite3 for example.
4 Comments
| in his name or password? Don't reinvent a square wheel.You can just hard code them in an array:
$passwords = ['someuser' => 'password'];
If you really want to store them in a file, so you can change them (for example) without editing the code, One way is to use something like this:
$passwords = ['someuser' => 'password'];
file_put_contents('passwords.php', '<?php return '.var_export($passwords,true).';');
This will create a file with something this in it (white space not withstanding):
<?php return array('someuser' => 'password');
Then when you need to import it into code you can simply use
$passwords = require 'passwords.php';
Which will put the contents of that file into that variable. Then you can check them really easily like so:
$passwords = require 'passwords.php';
if(isset($passwords[$user]) && $passwords[$user] == $password){
//do something when logged in
}
You can also modify the array and then save it:
$passwords = require 'passwords.php';
$passwords['someuser'] = $new_password;
file_put_contents('passwords.php', '<?php return '.var_export($passwords,true).';');
Of course you can even edit the passwords manually in the file. Sort of like a config file.
As I mentioned in the comments, it's better to use the DB, encryption and what not but as you said
this is only for me and someone else
As long as you don't have any third party data, and your ok with the security implications of this, then you can probably squeak by with the above.
To explain it:
Var Export converts arrays to valid PHP code, but in a string format. Then if we add the PHP open tag <?php, the return call return and the ending ; to it and save it in a PHP file, we now have a valid PHP file with dynamic passwords saved in it as an array.
When you have such a file that returns an array, you can inject that into a variable just by setting it like I showed above. Then it's a simple matter of checking to see if everything matches up.
Performance wise your offloading most of the penalty of this to saving the file, importing an array like this is very fast as is the key lookup. Much faster then iterating though a file and trying to parse the data from it.
You'll have to get the paths and filenames right and all that Jazz, but it should be pretty strait forward.
fread,fgetsetc. That saidusername and password then it stores it in a text fileis a bad idea for several reasons. Performance will be poor, security will be poor etc.this is only for me and someone elsewhy use a file at all you could just put them in an array right in the code. If this is not a publicly used site with no PCI, or confidential data and only your data, then you could probably get by with that as long as you know the risks of not having strong security. You can usefile_put_contents('passwords.php', '<?php return '.var_export($passwords, true).';');and then include them using$passwords = require 'passwords.php'