Jump to content

[SOLVED] Editting a txt file on my server...


Drags111

Recommended Posts

Ok heres what up:

 

I have a login form with username and password (html of course).

 

When you hit submit, I want the php script i have to make a log of that login.

 

Heres what I got for the html part:

 

<form action="login.php" method="post">
Username: <input type="text" name="usr" />
Password: <input type="password" name="pass" />
<input type="submit" />
</form>

 

I want the php's name to be login.php, and the text file's name is loginlog.txt

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/76702-solved-editting-a-txt-file-on-my-server/
Share on other sites

Also i want the log txt to look like this:

 

----------------------------------
Username: JohnDoe
Password: DoeJohn

----------------------------------
Username: JohnDoe
Password: DoeJohn

----------------------------------
Username: JohnDoe
Password: DoeJohn

----------------------------------
Username: JohnDoe
Password: DoeJohn

----------------------------------
Username: JohnDoe
Password: DoeJohn

----------------------------------
Username: JohnDoe
Password: DoeJohn

 

and so on :P

This is actually a very simple piece of code :-o I'll show you how to use it for appending (check it out online to see how it works).

 

<?php

$file = "url_to_text_file.txt";
$data = null;//clear off $data just in case it was cached somehow...
if(!is_writable($filename)) {//determine if we can write to the file first, we can't so let's die
die("I'm sorry, we encountered an internal error. Please contact the administrator about this problem.");
}

if(!$file = fopen($file,"a")){//open the file, check to see if we can or cannot
die("I'm sorry, the file cannot be opened. Please contact the administrator about this problem.");

//next, define what is to be added to the text file
//format of
//username
//password
//timestamp of log in (for other purposes)
//-------------------- (20 times)

$data .= $username."\n";
$data .= $password."\n";
$data .= time()."\n";
$data .= "----------------------------------------"."\n";

if(!fwrite($file,$data)){//we couldn't write to the file
die("I'm sorry, we could not write the data to the file. Please contact the administrator about this problem.");
}

echo "Success. You have written <textarea style='width:100%;height:100px;'>$data</textarea> into the file $filename.";

?>

 

This may look like a lot, but there's a lot of error catching and handling in here, it's better to be safe than sorry.

Oh, silly me, fwrite returns number of bytes, so I gotta fix it :-P This should work.

 

<?php

 

$file = "url_to_text_file.txt";

$data = null;//clear off $data just in case it was cached somehow...

if(!is_writable($filename)) {//determine if we can write to the file first, we can't so let's die

die("I'm sorry, we encountered an internal error. Please contact the administrator about this problem.");

}

 

if(!$file = fopen($file,"a")){//open the file, check to see if we can or cannot

die("I'm sorry, the file cannot be opened. Please contact the administrator about this problem.");

}

 

$data .= $username."\n";

$data .= $password."\n";

$data .= time()."\n";

$data .= "----------------------------------------"."\n";

 

if(fwrite($file,$data) === false){//we couldn't write to the file

die("I'm sorry, we could not write the data to the file. Please contact the administrator about this problem.");

}

 

echo "Success. You have written <textarea style='width:100%;height:100px;'>$data</textarea> into the file $filename.";

 

fclose($file);

 

?>

Shouldn't

if(!is_writable($filename))

be
if(!is_writable($file))
?

 

<?php

$file = "file.txt";
$data = null;//clear off $data just in case it was cached somehow...
if(!is_writable($file)) {//determine if we can write to the file first, we can't so let's die
die("I'm sorry, we encountered an internal error. Please contact the administrator about this problem.");
}

if(!$file = fopen($file,"a")){//open the file, check to see if we can or cannot
die("I'm sorry, the file cannot be opened. Please contact the administrator about this problem.");
}

$data .= $username."\n";
$data .= $password."\n";
$data .= time()."\n";
$data .= "----------------------------------------"."\n";

if(fwrite($file,$data) === false){//we couldn't write to the file
die("I'm sorry, we could not write the data to the file. Please contact the administrator about this problem.");
}

echo "Success. You have written <textarea style='width:100%;height:100px;'>$data</textarea> into the file $filename.";

fclose($file);

?>

What's your site link and the txt file link? See if this works;

 

<?php
$file = "test.txt";
$data = null;//clear off $data just in case it was cached somehow...
if(!$file = fopen($file,"a"))
{//open the file, check to see if we can or cannot
die("I'm sorry, the file cannot be opened. Please contact the administrator about this problem.");
}
$data .= $username."\n";
$data .= $password."\n";
$data .= time()."\n";
$data .= "----------------------------------------"."\n";

if(fwrite($file,$data) === false)
{//we couldn't write to the file
die("I'm sorry, we could not write the data to the file. Please contact the administrator about this problem.");
}
echo "Success. You have written <textarea style='width:100%;height:100px;'>$data</textarea> into the file $filename.";

fclose($file);

?>

Ok I tested your code, and It almost works. It says that i wrote 34556347 to the file, when in fact i did not even put that in. Then i went to check the http://gma.freehostia.com/loginlogs.txt and nothing was there. But then I went to the file manager and looked at the code and it said it was there. Anyway the point is, it posted the way wrong thing.

alright i ALMOST fixed the problem..

Drags111

test

1194707356

----------------------------------------

 

the numbers were the timestamp. but that doesnt look like its uh.... right. lol so anyway. we ALMOST got it down.

 

The other thing is that the text doesnt appear when u go to the link, but it does in the file manager.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.