Php .txt file Help

Ok, Im having some problem with this php program… It is supposed to act as a login screen. When register is clicked, the program will store whats in the username and password textbox in a .txt file, the problem is, it stores it in the .txt file but it doesnt go onto a new line. “\n” doesnt work. And when login is selected it also gives an error. Please help

$filel=“accounts.txt”;
$fhandle = fopen($filel, “a”) or die(“file cannot be opened”);

if(isset($_POST[‘register’])){
$name= $_POST[‘username’];
$password= $_POST[‘pass’];
fwrite($fhandle, $name."\n" .$password. “\n”);
}
fclose($fhandle);
//++++++++ word

if(isset($_POST[‘login’])){
$name= $_POST[‘username’];
$password= $_POST[‘pass’];
Login();
}

function login(){
$name = " “;
$array = explode(”\n", file_get_contents(‘accounts.txt’));

if(isset($_POST[‘login’])){
$name= $_POST[‘username’];
$password= $_POST[‘pass’];

for($i=0; $i< 4; $i=$i+2){
$ii=$i+1;
if($name == $array[$i] && $password == $array[$ii]){
echo “Access Granted”;

}

}}}

?>

USERNAME: PASSWORD:

Are you on Windows or Linux?

Well, you write to the file with “\n”.

This means ESCAPE-n, but, that is only used in certain instances when the item is displayed.
It is supposed to be used for telling the browser to use the actual value of the following character as a character instead of it’s “literal” value. The ESCAPE is used mostly in displaying the next character in the browser. It has NOTHING to do with data storage.

Therefore, you should use a different character for a delimiter. Comma’s are usually the default used.
Remember that the data you use inside of storage is only for your use, not the users. Unless you are planning on using the text file to display directly.

Most people use MySQL for username’s and passwords as TEXT files are easy to hack. Also, text processing is much more server demanding than a database query. If you have a lot of user’s, you server will be working harder than it needs to. (Text processing is about the worst thing for a server to do.)

One other issue is the way you test your values. Most would just store the array itself into the file. Since PHP creates all arrays as text, you can just write the actual array to the text file. And, just read it back as an array. Then, inside the checking code, just use something like " if ( in_array(accounts, $name ) " …
Would be much faster than running thru an entire array. Just an idea.

Hope that explains all of the items in your project. I suggest that you just use a comma instead of “\n”.

You need to use a program that recognises /n
notepad doesnt
notepad++ does

You also call Login(); when the function is login()

I would recommend using a database as ErnieAlex mentions.

Sponsor our Newsletter | Privacy Policy | Terms of Service