PHP FORMS....

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
fdost
Forum Newbie
Posts: 5
Joined: Wed Jun 26, 2002 9:45 am

PHP FORMS....

Post 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
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post 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");
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
fdost
Forum Newbie
Posts: 5
Joined: Wed Jun 26, 2002 9:45 am

Post 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
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Yes, because you can't have ANY output before using the header() function.
fdost
Forum Newbie
Posts: 5
Joined: Wed Jun 26, 2002 9:45 am

Post 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");
}

?>
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Make sure that there's no whitespace between the top of the file and the opening <?php.

Mac
Post Reply