Page 1 of 1

PHP FORMS....

Posted: Wed Jun 26, 2002 9:45 am
by fdost
Hi all, I am new to this list hope i get a good reception ;)
anyways here is my question:
I have an html form(name and password) form is posted to a PHP file, where i check the nam/password against my database. I want to be able to go back to login.html if user name does not exist or go to my main page if user login is successful? any idea how i can do that?

here is my code:
.....login.html......
<FORM action="validate.php" method="POST" name="frmLogin">
Login Name<BR>
<INPUT type="text" name="Name"></INPUT>
Password<BR>
<INPUT type="password" name="Password"></INPUT>
<INPUT type="submit" value="Login" ></INPUT>
</FORM>

.....validate.php......
<?php

//...search USER_INFO table for name and password...//
$query = "SELECT NAME, PASSWORD FROM USER_INFO WHERE NAME='$name' AND PASSWORD='$password'";
$result = pg_exec($connection, $query);
if (!$result) {
printf ("ERROR");
$errormessage = pg_errormessage($connection);
echo $errormessage;
.....here it should go back to login.html.....
exit;
}
$numrows = pg_numrows($result);
if($numrows < 1)
{
echo "No Data!";
.....here it should go back to login.html.....
exit;
}
.....here it should go to my main page.....
pgsql.pg_close();
?>

thank you for your help....
F

Posted: Wed Jun 26, 2002 9:56 am
by jason
You can easily make it so that any place you want them to go back to the login page, simply take out the echo statements, and do a

Code: Select all

header("Location: header.php");

Posted: Wed Jun 26, 2002 10:00 am
by twigletmac
Further to what Jason said (he was a bit quick for me), try this:

Code: Select all

<?php 
//...search USER_INFO table for name and password...// 
$name = $_POST&#1111;'name'];
$password = $_POST&#1111;'password'];

$connection = pg_connect("host=$host port=$port dbname=$db user=$user password=$password") or die(pg_errormessage());

$query = "SELECT NAME, PASSWORD FROM USER_INFO WHERE NAME='$name' AND PASSWORD='$password'"; 
$result = pg_query($connection, $query) or die(pg_errormessage()); 
if (pg_num_rows($result) == 0) &#123; 
	header('Location: login.html');
&#125; else &#123;
	header('Location: main.php');
&#125;

pg_close(); 
?>
I've never used PostgreSQL but had a search of the manual for these functions.

Mac

Posted: Wed Jun 26, 2002 10:21 am
by fdost
thank you all for responding...i tried as you said but i got this errro
Warning: Cannot add header information - headers already sent by (output started at d:\program files\apache group\apache\htdocs\php_test\validate.php:7)

i tested the validate.php with and without html head tags but got the save error.....anyidea why?

thanks

Posted: Wed Jun 26, 2002 10:32 am
by jason
Yes, because you can't have ANY output before using the header() function.

Posted: Wed Jun 26, 2002 10:44 am
by fdost
i removed all my echo calls, here is a full copy of my validate.php file...

<?php

$conn_string = "host=hostname port=5432 dbname=mydb user=name password=pass";
$connection = pg_connect($conn_string);

$key = "Name";
$name = $HTTP_POST_VARS[$key];

$key = "Password";
$password = $HTTP_POST_VARS[$key];

$query = "SELECT NAME, PASSWORD FROM USER_INFO WHERE NAME='$name' AND PASSWORD='$password'";
$result = pg_exec($connection, $query);

if (!$result) {
$errormessage = pg_errormessage($connection);
exit;
}
$numrows = pg_numrows($result);
pgsql.pg_close();

if($numrows < 1)
{
$go = "Login.html";
header("Location: $go");
}

?>

Posted: Thu Jun 27, 2002 1:32 am
by twigletmac
Make sure that there's no whitespace between the top of the file and the opening <?php.

Mac