...

View Full Version : PHP and HTML Verifying User Login



sruppert
04-22-2011, 12:42 AM
The assignment consists of four pages. The first is an html file which registers a user. The second is a php file which takes this information and writes the user name and login to a text file. Both of these work but I have them below for reference:

register.html

<html>
<head>
<title>Register Form</title>
<script language="javascript">

function validate(objForm)
{
str="";
if (objForm.firstName.value == "")
{
str+= "You must enter your First Name\n";
}
if (objForm.middleInitial.value == "")
{
str+= "You must enter a Middle Initial\n";
}
if (objForm.lastName.value == "")
{
str+= "You must enter your Last Name\n";
}
if (objForm.address.value == "")
{
str+= "You must enter an address\n";
}
if (objForm.city.value == "")
{
str+= "You must enter a city\n";
}
if (objForm.state.value == "")
{
str+= "You must enter a state\n";
}
if(isNaN(myForm.zipCode.value)||myForm.zipCode.value.length!=5)
{
str+= "You must enter a 5 digit zipcode\n";
}
if (objForm.username.value == "")
{
str+= "You must enter a user name\n";
}
if (objForm.password.value == "")
{
str+= "You must enter a password\n";
}

//print all form input
if (str != ""){
alert(str);
return false;
}
else{
str= "Please Confirm Your Information:";
str += "\nFirst Name: " +objForm.firstName.value;
str += "\nMiddle Initial: " +objForm.middleInitial.value;
str += "\nLast Name: " +objForm.lastName.value;
str += "\nAddress: " +objForm.address.value;
str += "\nCity: " +objForm.city.value;
str += "\nState: " +objForm.state.value;
str += "\nZip Code: " +objForm.zipCode.value;
alert(str);
return true;
}
}
</script>
</head>
<link rel="stylesheet" type="text/CSS" href="CellFormating.css"/>
<body>
<h2 align="center" class="CellFormat1"> Registration Page </h2>
<form id="CellFormat2" name="myForm" method="post" action="registerUser.php" onSubmit="return (validate(myForm))">
<table>
<tr>
<td>First Name: </td>
<td><input name="firstName" size="15" /></td>
</tr>
<tr>
<td>Middle Initial: </td>
<td><input type="text" name="middleInitial" size="1" /></td>
</tr>
<tr>
<td>Last Name: </td>
<td><input type="text" name="lastName" size="15" maxlength="20" /></td>
</tr>
<tr>
<td>Address: </td>
<td><input type="text" name="address" size="22" maxlength="30" /></td>
</tr>
<tr>
<td>City: </td>
<td><input type="text" name="city" size="10" maxlength="15" /></td>
</tr>
<tr>
<td>State: </td>
<td><input type="text" name="state" size="10" maxlength="15" /></td>
</tr>
<tr>
<td>Zip Code: </td>
<td><input type="text" name="zipCode" size="5" maxlength="10" /></td>
</tr>
<tr>
<td>User Name: </td>
<td><input type="text" name="username" size="15" maxlength="20"></td>
</tr>
<tr>
<td>Password: </td>
<td><input type="password" name="password" size="8"/></td>
</tr>
<tr>
<td><input type="submit" name="Login" value="Submit and Login" onSubmit="return (validate(myForm))"/></td>
</tr>
</table>
</form>
</body>
</html>

registerUser.php

#!/usr/local/Free/bin/php #
<html>
<head>
<title>Register User</title>
</head>
<link rel="stylesheet" type="text/CSS" href="CellFormating.css"/>
<body>
<?php
if("Login"){
$file=fopen("UserData.txt","a+") or exit("Unable to open file");
fwrite($file,$username.",".$password."\n");
}
fclose($file);
?>
<h2 align="center" class="CellFormat1"> Thanks for registering!!! </h2>
<table align="center">
<tr>
<td><a href="login.html">Login</a></td>
<td><a href="home.html">Go Home!</a></td>
</tr>
</table>
</body>
</html>

The third is an html login page:
login.html

<html>
<head>
<title>Login Form</title>
<script language="javascript">

function validate(objForm)
{
str="";
//VALIDATING CONTACT INFORMATION
if (objForm.username.value == "")
{
str+= "You must enter a User Name\n";
}
if (objForm.password.value == "")
{
str+= "You must enter a password\n";
}
//print all form input
if (str != ""){
alert(str);
return false;
}
}
</script>
</head>
<link rel="stylesheet" type="text/CSS" href="CellFormating.css"/>
<body>
<h2 align="center" class="CellFormat1"> Login Page </h2>
<form id="CellFormat2" name="myForm" method="post" action="verifyLogin.php" onSubmit="return (validate(myForm))">
<p align="center">
<table>
<tr>
<td>User Name: </td>
<td><input type="text" name="username" size="15"/></td>
</tr>
<tr>
<td>Password: </td>
<td><input type="password" name="password" size="15"/></td>
</tr>
<tr>
<td><input type="submit" name="Login" value="Login" onSubmit="return (validate(myForm))"/></td>
</tr>
</table>
</form>
</body>
</html>

The last page is a php page which is supposed to open text file and determine if the user name a password are correct. If they are, the user is redirected to an order form. If they are not, the user is sent to an error page. No matter what I do the user is sent to an error page. The 'printf checks' show that the file is opened and read through, but not correctly getting the values entered by the user and checking them.


#!/usr/local/Free/bin/php #
<html>
<head>
<title>Verify User Information</title>
</head>
<body>
<?php

$URL1="order.html";
$URL2="errormessage.html";

$file = fopen("UserData.txt", "r") or exit("Unable to open file!");
printf($username.$password);

if ($Login)
{
printf("1");
while(!feof($file))
{

printf("2");
$str=fgets($file);
$values=split(",",$str);
$values[0] = trim($values[0]);
$values[1] = trim($values[1]);
printf("3");
if($values[0] == "" || $values[1] == "") {break;}
printf("4");
if($username == $values[0] && $password == $values[1])
{
printf("5");
header ("Location: $URL1");
fclose($file);
exit;
}
}
}
printf("6");
header("Location: $URL2");
?>
</body>
</html>

oesxyl
04-22-2011, 01:13 AM
in the last page:

#!/usr/local/Free/bin/php #
<html>
<head>
<title>Verify User Information</title>
</head>
<body>
<?php

$URL1="order.html";
$URL2="errormessage.html";

$file = fopen("UserData.txt", "r") or exit("Unable to open file!");
printf("username is: '%s' and password is: '%s'",$username,$password);

if ($Login)
{
printf("1");
while(!feof($file))
{

printf("2");
$str=fgets($file);
$values=split(",",$str);
$values[0] = trim($values[0]);
$values[1] = trim($values[1]);
printf("3");
if($values[0] == "" || $values[1] == "") {break;}
printf("4");
if($username == $values[0] && $password == $values[1])
{
printf("5");
header ("Location: $URL1");
fclose($file);
exit;
}
}
}
printf("6");
header("Location: $URL2");
?>
</body>
</html>

best regards

sruppert
04-22-2011, 06:32 PM
I was told that I could be using incorrect indexes from the values[] array. What does this mean?

tracknut
04-22-2011, 06:45 PM
I was told that I could be using incorrect indexes from the values[] array. What does this mean?

I suspect it means the person who told you that didn't know what the issue was... :)

At the top of this verify login routine, you print out the values of $username and $password. What are they there, and is it what you expect? How are they getting set - I can't see anything before them that sets them, presumably to what the user entered. Specifically, you are submitting them as $_POST variables in the form (login.html), but never reading them out of the $_POST array in the verify routine. Look up how POST works, hopefully it will be clear.

Dave

oesxyl
04-22-2011, 06:49 PM
I was told that I could be using incorrect indexes from the values[] array. What does this mean?
incorrect because are undefined? who told this should told also what he/she mean by 'incorrect'.

best regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum