douziqian2871
douziqian2871
2014-03-11 21:51

需要一个用户小编辑的登录用户名和密码

已采纳

Firstly, l am very new to creating html and php codes in websites, so if this may seem a trivial question to some on here please appreciate that l require a bit of help. Thanks

I have created a login username and password form for just one user (html form) to work in conjunction to a .php code, it works perfectly.

Current:

  1. It will not allow the form to submit if either username and/or password fileds are blank.
  2. incorrect username and/or password displays the echo ("incorrect"); text on the www.......com/login.php page.
  3. When both username and password filed are entered correctly it redirects to the required page.

EDIT WANTED:

  1. Instead of the displaying the echo ("incorrect"); text on the www.......com/login.php page. I want "incorrect" to to be displayed underneath or within the html form. This saves going back to the original page each time to redo the entries.

Thank you.

HTML FORM:

  <fieldset>

        <legend>Login</legend>

        <label for="username" >UserName: </label>
        <input type="text" id="username" name="username" required="true" />

        <label for="pass" >Password:</label>
        <input type="password" id="pass" name="pass" required="true"/>

        <input type="submit" name="Submit" value="Submit" />
         <include'login.php'/>
     </fieldset>

</form>

login.php

<?php

       if(($_POST['username'] == "user1") && ($_POST['pass'] == "pass1"))

    { 

     header("Location:http://www.yahoo.com");

    }


    else


    {
      echo ("incorrect");

    }


    ?>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • doudu161481 doudu161481 7年前

    Nest the form inside the else condition then the login is handled in a single file

    <?php
         if(($_POST['username'] == "user1") && ($_POST['pass'] == "pass1"))
      { 
        header("Location:http://www.yahoo.com");
      }
     else
     { ?>
    
    <form name= "login" method="post" accept-charset='UTF-8'>
    <fieldset>
        <legend>Login</legend>
        <label for="username" >UserName: </label>
        <input type="text" id="username" name="username" required="true" />
        <label for="pass" >Password:</label>
        <input type="password" id="pass" name="pass" required="true"/>
        <input type="submit" name="Submit" value="Submit" />
         <include'login.php'/>
     </fieldset>
    </form>
    
    <?php 
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            echo "incorrect";
          }
     }
    ?>
    
    点赞 评论 复制链接分享
  • duanlinjian5819 duanlinjian5819 7年前

    instead of

    {
    echo "incorrent";
    }
    

    type:

    {
    header("Location: loginform.php?error=incorrect");
    }
    

    and in your login form .php something like:

    if(isset($_GET['error'])) {
        echo $_GET['error'];
    }
    

    Or even better: you could save your form as a .php . And start the form with

    if(isset($_POST)) 
    

    do you post logic.

    then display the form.

    and in the input vars, type

    <INPUT value="<?php echo isset($_POST['username']) ? $_POST['username'] :''; ?>">
    

    EDIT: To summarize:

    <?php if(isset($_POST)) {
    
         if(check) { 
            header("Location ...);
         } else { 
           $error = 'incorrect';
         }
    
    } else {
       $error = '';
    }
    ?>   
    <form action="<?php echo $_SERVER['PHP_SELF'];?>">
    <fieldset>
    
        <legend>Login</legend>
    
        <label for="username" >UserName: </label>
        <input type="text" value="<?php echo isset($_POST['username']) ? $_POST['username] : ''; ?>" id="username" name="username" required="true" />
        <label for="pass" >Password:</label>
        <input type="password" id="pass" name="pass" required="true"/>
    
        <input type="submit" name="Submit" value="Submit" />
     </fieldset>
    
     </form>
     <?php echo $error; ?>
    
    点赞 评论 复制链接分享

相关推荐