Login Authentication
Moderator: General Moderators
Login Authentication
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
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
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)?
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)?
I want to (3) Query the Database (4) Get the results and want to use (5) Sessions
Here is the code
Thanx
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>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");
?>Well, here's some basic code that can go before your html form:
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.
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;
}
}
}
?>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>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";
}
?>- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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.";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
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
Last edited by Kingo on Tue Aug 31, 2004 1:42 pm, edited 1 time in total.
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)
)- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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.