I hv make an html page on which there r 2 fields of username n pw n a login button.For Authentication code I make another file named "check.php".Kindly guide me that hw can I give the refernce of the form objects in this file as I need the values of username n pw in order to check.

mysql_select_db("alauddin", $con);
$name=form1.txtusername;
$password=form1.txtpw;
$result = mysql_query("SELECT username,pw FROM login");
while($row = mysql_fetch_array($result))
  {
 if($name=$row['username'] && $password=$row['pw'])
 echo "You are logged in";
 else
 echo "Enter again";
  }

Its giving error on the following lines as its the form1 in the html file:

$name=form1.txtusername;
$password=form1.txtpw;

I include the file "check.php" in the html file.
Reply soon..Regards,

Dani AI

Generated

A few concrete points based on ’s code and ’s reply.

Common mistakes causing the behavior seen here

  • Reading form data fails when the page that includes the PHP is not served/parsed by PHP or when the form posts to the wrong URL. Make the page a PHP page or post the form to a PHP script.
  • Using the assignment operator where you meant to compare will always produce the wrong result. Also, looping over every DB row to find a match is inefficient and unsafe.
  • Never store plain-text passwords and avoid building SQL with raw input (SQL injection).

A safe, practical workflow

  1. Have the form post to a PHP script (or handle it in the same .php page).
  2. Read and validate the inputs (use filter_input() or explicit checks).
  3. Query the database for the specific username using a prepared statement (no client-side looping through all rows).
  4. Verify the password using the built-in password verification routine.
  5. Use exit/die to stop script execution where appropriate; use break only to exit loops or return to exit a function.

Example (modern, minimal) — replace DSN/user/pass and column names as needed:

<?php
$username = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_STRING);
$password = filter_input(INPUT_POST, 'password'); // keep raw for verify

$pdo = new PDO('mysql:host=localhost;dbname=alauddin;charset=utf8mb4', 'dbuser', 'dbpass', [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]);

$stmt = $pdo->prepare('SELECT pw_hash FROM login WHERE username = :username LIMIT 1');
$stmt->execute(['username' => $username]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);

if ($row && password_verify($password, $row['pw_hash'])) {
    echo 'You are logged in';
    exit;
}

echo 'Enter again';
exit;
?>

Useful references: PDO prepared statements, password_verify, filter_input.

Recommended Answers

All 4 Replies

Hello.

If you have form like this:

<form action="check.php" method="post">
<input type="text" name="username" value="">
<input type="text" name="password" value="">
<input type="submit" name="submit">
</form>

...you can access form values like this:

$_POST['username'] // username
$_POST['password'] // password

- Mitko Kostov

Thankx for ur guidance.Is there any function in php which works as Break function in c++.As I want to terminate the execution at a particular point.

Regards,

Hello.

If you have form like this:

<form action="check.php" method="post">
<input type="text" name="username" value="">
<input type="text" name="password" value="">
<input type="submit" name="submit">
</form>

...you can access form values like this:

$_POST['username'] // username
$_POST['password'] // password

- Mitko Kostov

Is there any error in the code I posted b4 as its not giving the correct results.Is its correct?

while($row = mysql_fetch_array($result))
  {
 if($name=$row['username'] && $password=$row['pw'])
 echo "You are logged in";
 else
 echo "Enter again";
  }

Hello.

If you have form like this:

<form action="check.php" method="post">
<input type="text" name="username" value="">
<input type="text" name="password" value="">
<input type="submit" name="submit">
</form>

...you can access form values like this:

$_POST['username'] // username
$_POST['password'] // password

- Mitko Kostov

Can u plzz check the code I posted b4 n let me know if thers any errors as its not giving the correct results:

while($row = mysql_fetch_array($result))
  {
 if($name=$row['username'] && $password=$row['pw'])
 echo "You are logged in";
 else
 echo "Enter again";
  }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.