0

Please help with php. I had this code working before when I had a separate php file and separate html file, but for some reason now it's not working when I try to combine them together into index.php. this is the error that i'm getting:

Undefined index: username

Undefined index: password

I tried using if(isset($_POST['submit'])) for $username/$password but this didn't change anything

<html>
<body>

<form action="index.php" method="post">
<table border="0">
<tr bgcolor="#cccccc">
<td width="150">Username: </td>
<td width="20"><input type="text" name="username" size="15"
maxlength="15"></td>
</tr>
<tr>
<tr bgcolor="#cccccc">
<td width="150">Password: </td>
<td width="20"><input type="password" name="password" size="15"
maxlength="15"></td>
</tr>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Login"></td>
</tr>

</table>
</form>
</body>
</html>
<?php


$username = htmlspecialchars($_POST['username']);
$password = htmlspecialchars($_POST['password']);
trim($username);
trim($password);

$usernameArr = array( 1 => "a",
2 => "b",
3 => "c",
4 => "d",
5 => "e",
6 => "f",
7 => "g",
8 => "h",
9 => "i",
10 => "j");
$passwordArr = array( 1 => "pass1",
2 => "pass2",
3 => "pass3",
4 => "pass4",
5 => "pass5",
6 => "pass6",
7 => "pass7",
8 => "pass8",
9 => "pass9",
10 => "pass0");
$varBool = false;
for ( $i = 1; $i <= 10; $i++){
if ($username == $usernameArr[$i] && $password === $passwordArr[$i]){
$varBool = true;
}
}
If ($varBool){
echo "You have successfully logged in!";
}
else{
echo "Sorry, wrong information has been entered!";
}

?>
Mr. Pyramid
  • 3,855
  • 5
  • 32
  • 56
Alex
  • 109
  • 2
  • 11

2 Answers2

0

use isset function before $_POST variables . and add name="submit" in submit button .Like this

<input type="submit" name="submit" value="submit">

<?php 
if(isset($_POST['submit']))
{
  // your code here
}
Gaurav
  • 442
  • 3
  • 7
0

You need to put conditions for the post request. Here is your code when this index.php refresh than it will try to get username and password but it will not get. So here you need to put conditions like isset.

so your php code is like,

<?php
   if (isset($_POST)) {
      $username = htmlspecialchars($_POST['username']);
      $password = htmlspecialchars($_POST['password']);
      trim($username);
      trim($password);

      $usernameArr = array( 1 => "a",
        2 => "b",
        3 => "c",
        4 => "d",
        5 => "e",
        6 => "f",
        7 => "g",
        8 => "h",
        9 => "i",
        10 => "j");
      $passwordArr = array( 1 => "pass1",
         2 => "pass2",
         3 => "pass3",
         4 => "pass4",
         5 => "pass5",
         6 => "pass6",
         7 => "pass7",
         8 => "pass8",
         9 => "pass9",
         10 => "pass0");
       $varBool = false;

      for ( $i = 1; $i <= 10; $i++){
        if (
             $username == $usernameArr[$i] && 
             $password === $passwordArr[$i]
        ){
            $varBool = true;
        }
      }
      If ($varBool){
         echo "You have successfully logged in!";
      } else{
        echo "Sorry, wrong information has been entered!";
      }

   }
?>

I think it will help you. Once user logged in successfully than redirect to another page or refresh this current page so when you refresh this page then it will not ask for the confirmation of the post request.

Akib
  • 193
  • 1
  • 2
  • 13