Page 1 of 1

Login Validation

Posted: Mon Apr 14, 2008 12:12 am
by php14user
Hi,

I have a login section in my home page.
When I try to login using valid credentials and click on "Submit" button, it opens a new page with user information.
Now I would like to validate whether user is valid or not.
So What I would like to do is, when somebody tries to login using invalid credentials then my homepage should not be redirected to any other page, instead, there should be a message in the login section (next to user name and Password textboxes) of the home page that "Invalid Username/Password".

Another question is:
Let's say there is a login section in my home page.
Now if user log in using valid credentials then he should be redirected to Page1.php.
Else if the login credentials are invalid then he should be redirected to Page2.php.
(I would like to use a separate php file to access database)
How do I accomplish these?

Thanks.

Re: Login Validation

Posted: Mon Apr 14, 2008 5:57 am
by onion2k
There are two approaches.

1. Submit the login form to the login page, and check the user is valid. If they are forward the user to the second page. If they're not display the login form again with the "User not valid" message.

2. Submit the login for to the second page. If the user is valid display the second page. If the user is not valid forward the user back to the login form.

Personally I use method 1, but it can affect the way the browser's back button works so you get "Page Expired" messages occasionally. I suggest trying both and using whichever suits you best.

Re: Login Validation

Posted: Tue Apr 15, 2008 4:44 am
by php14user
Hi,

Thanks for the suggestion.
It would be helpful if you let me know how do we display another page if there is a valid user?

In my home page I have included "get_valid_user.php" that checks the user validity upon submission of login form (that reloads the home page).
If user is not valid then "get_valid_user.php" displays the message on the home page (in Login section).
Now if user is valid then I would like to display user information in a separate page. How do I do it?

Here is the piece of code I am using:


<?php


if($_POST["submit_form"] == "login_form_submitted")

{

if(!isset($_FILES) && isset($HTTP_POST_FILES))
{
$_FILES = $HTTP_POST_FILES;
}


$con = mysql_connect("localhost","root","xyz");
if (!$con)
{
die('Could not connect to MySQL: ' . mysql_error());
}

mysql_select_db("my_db", $con);

$login_id = $_POST["login_id"];
$login_password = $_POST["login_password"];

$result3 = mysql_query("SELECT * FROM Company_Registration where login_id = '$login_id' && login_password = '$login_password'");

$no_Of_Row_Affected = mysql_affected_rows();

if ($no_Of_Row_Affected == 0)
{
echo "No user!!";
}

else
{

//header('Location:http://localhost/Site/user_window.php');
//include("user_window.php");
I need to include a code here to open a new page user_window.php.

}

}

?>

FYI, when I use "include("user_window.php");" then the content of user_window.php and content of home page overlaps.
I want to open the content of user_window.php in a separate page.

And when I use "header('Location:http://localhost/Site/user_window.php');", I get following error message:

Warning: Cannot modify header information - headers already sent by (output started at /Library/WebServer/Documents/Site/home_page.php:5) in /Library/WebServer/Documents/Site/get_valid_user.php on line 40

Re: Login Validation

Posted: Tue Apr 15, 2008 7:57 am
by php14user
Somebody please help!!

Thanks.