Page 1 of 3

Login Authentication

Posted: Tue Aug 31, 2004 12:43 pm
by Kingo
Hello,
I have a user name and password field in my form. When I click the submit button , i want to check for the authentication of user name and password that are present in the database.
I'm new to PHP so donot know how it works.
Any help is appreciated.

Thanx

Posted: Tue Aug 31, 2004 12:49 pm
by markl999
Might be easier to break it down into 'chunks' and have a go at each one and ask for help where you get stuck on each bit as user authentication is quite a large topic in itself.
The chunks might be:

1. See if the user filled the form (presssed submit)
2. Validate the username and password (check for empty fields, strange characters, etc)
3. Query the database
4. Get the database results
5. If the username and password was correct what then? Do you want to use sessions to persist the login (so they don't have to keek logging in etc)?

Posted: Tue Aug 31, 2004 12:54 pm
by Kingo
I want to (3) Query the Database (4) Get the results and want to use (5) Sessions

Here is the code

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Login</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form name="form1" method="post" action="login.php">
  <p align="center"><strong><font color="#FF0000" size="2" face="Arial, Helvetica, sans-serif"><u>Login 
    Information</u></font></strong></p>
  <p align="center"><font size="2" face="Arial, Helvetica, sans-serif"><strong> 
    Name: 
    <input type="text" name="name">
    </strong></font></p>
  <p align="center"><font size="2" face="Arial, Helvetica, sans-serif"><strong>Password: 
    <input type="password" name="pass">
    </strong></font></p>
  <p align="center"><strong><font size="2">
    <input type="submit" name="Submit" value="Submit">
    </font></strong></p>
  <p><br>
  </p>
  </form>
</body>
</html>
Thanx

Posted: Tue Aug 31, 2004 12:57 pm
by Kingo
Here is my mysql_conn.php file

Code: Select all

<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_mysql_conn = "localhost";
$database_mysql_conn = "php_proj";
$username_mysql_conn = "abc";
$password_mysql_conn = "123";
$mysql_conn = mysql_pconnect($hostname_mysql_conn, $username_mysql_conn, $password_mysql_conn) or die(mysql_error());
@MYSQL_SELECT_DB("$database_mysql_conn") OR DIE("Unable to select database"); 
?>

Posted: Tue Aug 31, 2004 1:05 pm
by markl999
Well, here's some basic code that can go before your html form:

Code: Select all

<?php
require_once 'mysql_conn.php';
session_start();
if(!empty($_SESSION['loggedin'])){
  echo 'You are already logged in';
  exit;
}
if(!empty($_POST['Submit'])){
  $name = trim($_POST['name']);
  $pass = trim($_POST['pass']);
  if(!empty($name) && !empty($pass)){
    $sql = "SELECT id FROM ".DBNAME." WHERE name='$name' AND pass='$pass'";
    $res = mysql_query($sql) or die(mysql_error());
    if(mysql_num_rows($res)){
      $_SESSION['loggedin'] = 1;
      header("Location: login.php");
      exit;
    }
  }
}
?>
It should give you an idea of what's involved and you'll probably need to tweak some of the variables to match your setup. It's a rough guide that you should be able to build on.

Posted: Tue Aug 31, 2004 1:22 pm
by Kingo
I'm getting a blank page if i use this code. I donot know where I'm making mistake

Code: Select all

<html>
<head>
<title>Login</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php 
// Connection to Host and Database
require_once('Connections/mysql_conn.php'); ?>

<?
$username = $_POST["name"];
$password = $_POST["pass"];

$result = MYSQL_QUERY("SELECT * from constant WHERE Uname='$username'and Password='$password'")
   or die ("Name and password not found or not matched");

$worked = mysql_fetch_array($result);

/*
$username = $worked[username];
$password = $worked[password];
*/

if($worked)
   echo "Welcome $user! "; 

?>

</body>
</html>

Posted: Tue Aug 31, 2004 1:24 pm
by markl999
There's no form there?
The code i provided was really meant to go at the top of your login page, right before the <html> tag, but you still need the rest of the html, the form etc..

Posted: Tue Aug 31, 2004 1:27 pm
by Kingo
I get a blank page if I enter invalid username or invalid password

Posted: Tue Aug 31, 2004 1:28 pm
by markl999
Repost the exact code you have now.

Posted: Tue Aug 31, 2004 1:30 pm
by Kingo
It works now. Here is the Code.

Code: Select all

<?php 
// Connection to Host and Database
require_once('Connections/mysql_conn.php'); 

$username = $_POST["name"];
$password = $_POST["pass"];

$result = MYSQL_QUERY("SELECT * from constant WHERE Uname='$username'and Password='$password'")
   or die ("Name and password not found or not matched");

$worked = mysql_fetch_array($result);

if($worked)
   {
   echo "Welcome $username!";  
   }
   
   else
   {
   echo "The Username and Password donot match";
   }

?>

Posted: Tue Aug 31, 2004 1:32 pm
by feyd
looks like it'll be an empty page, not a blank page.

Code: Select all

if(mysql_num_rows($result))
  echo "Welcome $username! ";
else
  echo "You were not recognized.";

Posted: Tue Aug 31, 2004 1:32 pm
by Kingo
Now I need help from you.

Instead of Welcome $username I want to display the following.

I want to display all the values of the fileds in Text boxes. i.e, i want the values from the database to be populated on a form with a SUBMIT button

Posted: Tue Aug 31, 2004 1:34 pm
by Kingo
My table has the following fields;

Code: Select all

CREATE TABLE constant ( 
ID INT(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
Uname varchar(15),
Password varchar(15),
cvalue1 varchar(10),
cvalue2 varchar(10),
dvalue1 varchar(10),
dvalue2 varchar(10)
)

Posted: Tue Aug 31, 2004 2:18 pm
by Kingo
I wanted to know "How to assign values from the Database to the Form"

Posted: Tue Aug 31, 2004 2:34 pm
by feyd
basics: perform the query, parse out the field names into the form element names you want to use and texts, then fill the elements with your data.. a simple looping block with a foreach or for can handle that. I'd use an associative array to transform the names of fields into form element names, types, and their plain-text side comments.