-1

I tried a simple code for making a login. It works perfectly fine when I submit details and give a correct response on the screen but when I load it for first time on browser it displays an error message saying:

Notice: Undefined index: user in C:\wamp\www\test\first.php on line 14
and
Notice: Undefined index: user in C:\wamp\www\test\first.php on line 15

Can't identify the problem it has.

<html>
    <head>
        <title>My First PHP Program!</title>
         <link type="text/css" rel="stylesheet" href="stylesheet.css"/>
    </head>

    <body>
        <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
          <p>username <input type="text" name="usser"/></p>
          <p>password <input type="password" name="pass" /></p>
          <p>   <input type="submit" name="submit"  /></p>
        </form>
        <?php 
            $name = $_REQUEST['usser']; 
            $pas=$_REQUEST['pass'];
            if($name=="mandeep"&&$pas=="mani"){
                echo "successful";
            }
            else{
                echo "Login Failed";
            }
       ?>
    </body>
</html>
0

3 Answers 3

2

You need to verify if that variable is setted first. And I recommend you to use $_POST variables if you are using post action. And use === for identical comparisons.

    <?php 
        if (isset($_POST['usser']) && isset($_POST['pass'])){
            $name = $_POST['usser']; 
            $pas= $_POST['pass'];
            if($name === "mandeep" && $pas === "mani"){
                echo "successful";
            }
            else{
                echo "Login Failed";
            }
        }
   ?>

POST php reference
PHP comparison operators

3
  • Probably because you're dumping code without explaining what you've changed (before edit), answering a question that has been answered many times before and not explaining what else is wrong with this code.
    – CodeCaster
    Jun 21, 2014 at 13:43
  • cool..this helped..bt can u tell me d diff between using $_POST and $_REQUEST?
    – developer
    Jun 21, 2014 at 13:49
  • @user3185620 Requests gets the value of $_GET, $_POST and $_COOKIE variables
    – hlscalon
    Jun 21, 2014 at 13:51
0

You have to check for new post for form

<?php 
if (!empty($_POST)) {
    $name = $_REQUEST['usser']; 
    $pas=$_REQUEST['pass'];
   if($name=="mandeep"&&$pas=="mani"){
  echo "successful";
   }
   else{
   echo "Login Failed";
   } }
   ?>
0

As the page loads you still run the code in the php block after the form.

You could use isset to validate that it's been set.

Not the answer you're looking for? Browse other questions tagged or ask your own question.