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
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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);

 

?>

Link to comment
Share on other sites

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);

?>

Link to comment
Share on other sites

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);

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.