Hello,

I am trying to make a simple PHP web page as an assignment with register and login form and I am stuck in asking PHP to verify the password when it is wrong. It basically recognizes the ID & PASSWORD when they are right and send me through to the next page but when those inforamtion above are wrong I am not able to tell it "failed login" I am a newbie and your help would be very useful. I thank you in advance.

The url: http://www.dcs.bbk.ac.uk/~pparod01/index.php

This is my code:

<?php

$username = 'username';
$password = 'password';

 if (isset($_POST["username"]) && isset($_POST["password"])) {
                
	// open text.txt for reading
	$file = fopen("text.txt","r");

	while (!feof($file)) {
		$data = explode ("|", fgets($file));
		if(isset($data[0]) && isset($data[1])){
			if (trim($data[0]) == trim($_POST["username"]) && trim($data[1]) == trim($_POST["password"])) {
				$login = true;
				$_SESSION["login"] = $login;
				$_SESSION["username"] = $_POST["username"];
				//$_SESSION['type'] = $data[3];
				echo "Thank you for logging in, in 5 seconds you will be taken to the homepage.";
                header("refresh: 5; private.php");
				break;
				    }
					
					
                                }
                                
                        }
                        fclose($file);
                }

		
		?>



<form action="<?php $_SERVER['PHP_SELF'];?>" method="post">
<label>Login</label>
  <fieldset>
  <table>
  
    <tr align="right">
      <td><label>Username:</label></td>
      <td><input name="username" type="text" id="user"></td>
    </tr>
    <tr align="right">        
      <td><label>Password:</label></td>         
      <td><input type="password" name="password" id="pword"></td>  
    </tr>   
    <tr>        
      <td></td>        
      <td><input type="submit" name="login" id="login" value="login"> 
        <a href="http://www.dcs.bbk.ac.uk/~pparod01/register.php">Register</a></td>          
    </tr>
	
  </table>
  </fieldset>

Recommended Answers

All 9 Replies

what kind of database are you using?

I am not using any database: ID & PASSWORDS are saved in text.txt and then I recall the file with fopen. It's an assignment for the college it's not a proper website. Thank you!

I'm not so sure, but try this:

<?php
$username = 'username';
$password = 'password';

if (isset($_POST["username"]) && isset($_POST["password"]))
{

   // open text.txt for reading
    $file = fopen("text.txt","r");

   while (!feof($file))
   {
      $data = explode ("|", fgets($file));
      if(isset($data[0]) && isset($data[1])){
      if (trim($data[0]) == trim($_POST["username"]) && trim($data[1]) ==     trim($_POST["password"]))
      {
          $login = true;
          $_SESSION["login"] = $login;
          $_SESSION["username"] = $_POST["username"];
          //$_SESSION['type'] = $data[3];
          echo "Thank you for logging in, in 5 seconds you will be taken to the     homepage.";
          header("refresh: 5; private.php");
          break;
        }
      else
         {
              // something you want if the log in failed.
         }

    }

}
fclose($file);
}
?>

It's almost right the else

         {
              // something you want if the log in failed.
         }

and I have tried it myself before but when It appear (so it means that the login is wrong) It write it down continusly: "Login FailedLogin FailedLogin FailedLogin FailedLogin FailedLogin FailedLogin FailedLogin FailedLogin FailedLogin FailedLogin FailedLogin FailedLogin FailedLogin FailedLogin FailedLogin FailedLogin FailedLogin FailedLogin FailedLogin Failed".

Or when the login is right it says "Login FailedLogin FailedThank you for logging in, in 5 seconds you will be taken to the homepage."

I modify the code again. ^^,

<?php
$username = 'username';
$password = 'password';
 
if (isset($_POST["username"]) && isset($_POST["password"]))
{
 
   // open text.txt for reading
    $file = fopen("text.txt","r");
 
   while (!feof($file))
   {
      $data = explode ("|", fgets($file));
   }
      if(isset($data[0]) && isset($data[1]))
      {
          if (trim($data[0]) == trim($_POST["username"]) && trim($data[1]) ==     trim($_POST["password"]))
          {
             $login = true;
             $_SESSION["login"] = $login;
             $_SESSION["username"] = $_POST["username"];
             //$_SESSION['type'] = $data[3];
             echo "Thank you for logging in, in 5 seconds you will be taken to the      homepage.";
             header("refresh: 5; private.php");
          break;
        }
      else
         {
              // something you want if the log in failed.
         }
 
    }
 

fclose($file);
}
?>
<?php
  $username = 'username';
  $password = 'password';
  if (isset($_POST["username"]) && isset($_POST["password"])) 
  {
    // open text.txt for reading
    $file = fopen("text.txt","r");
    $login = false;
    while (!feof($file)) 
    {
      $data = explode ("|", fgets($file));
      if(isset($data[0]) && isset($data[1]))
      {
        if (trim($data[0]) == trim($_POST["username"]) && trim($data[1]) == trim($_POST["password"])) 
        {
          $login = true;
          $_SESSION["login"] = $login;
          $_SESSION["username"] = $_POST["username"];
          echo "Thank you for logging in, in 5 seconds you will be taken to the homepage.";
          header("refresh: 5; private.php");
          break;
        }
      }
    }
    if (!$login)
    {
      echo "Not logged in.";
    }
    fclose($file);
  }
?>

It's almost done but it gives error in this line: if (!$login). It is working but it appears:

Notice: Undefined variable: login in ./login.php on line 28
Not logged in.

I have tried myself:

if {
  $login = false;
  echo "Not logged in.";
}

It doesn't work either.

Move the line $login = false; from line 28 to between 3 and 4.

It should work, perhaps you did not copy everything.

Thank you very much endeed for your help maid!

It's working fine.

Paolo

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.