0

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>

Edited by __avd: Added [code] tags.

4
Contributors
9
Replies
10
Views
6 Years
Discussion Span
Last Post by paolonewage
0

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!

0

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);
}
?>
0

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."

Edited by ~s.o.s~: formatting

0

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);
}
?>
0
<?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);
  }
?>
0

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.

Edited by ~s.o.s~: formatting

0

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

It should work, perhaps you did not copy everything.

Edited by pritaeas: n/a

0

Thank you very much endeed for your help maid!

It's working fine.

Paolo

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.