简单的基本用户名和密码代码,1个用户无法正常工作

时间:2014-03-10 16:04:45

标签: php html5 forms login username

我非常擅长制作网站,而且我还在学习。今天我花了很多时间试图弄清楚我试图使用的这段代码有什么问题。任何帮助深表感谢。

主要问题是,在提交错误或正确的用户名/密码后,转到空白页面并且不显示错误的用户名/密码消息。谢谢。史蒂夫

HTML

<form id='login' action='login.php' method='post' accept-charset='UTF-8'>
    <fieldset>
        <legend>Login</legend>
        <input type='hidden' name='submitted' id='submitted' value='1'/>

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

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

        <input type='submit' name='Submit' value='Submit' />
    </fieldset>
</form>

PHP

<?php

    session_start();

    if(isset($_POST['username'])){ 

        if(($_POST['username'] == "admin") && ($_POST['password'] == "adminpass"))
        {
            $_SESSION['secured'] = "Secured";
        }
        else
        {
            echo "Oops! Looks like the username and password is not what I'm looking for, sorry you can't continue <p><a href='?'>Lets try that again...</a>";
        }
    }


if(!isset($_SESSION['secured']))
{
    echo "<form method='post'>

    <form id='login' action='login.php' method='post' accept-charset='UTF-8'>
        <fieldset>
            <legend>Login</legend>
            <input type='hidden' name='submitted' id='submitted' value='1'/>

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

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

            <input type='submit' name='Submit' value='Submit' />    
        </fieldset>
    </form>
}
else
{
    ?>


<html>
    <head>
        <title>Session Login</title>
    </head>
    <body>
        <p>You have been successfully logged in ... </p>
    </body>
</html>


<?php 
    }
?>

1 个答案:

答案 0 :(得分:0)

您的php代码部分有一些语法/问题

  • name="UserName"$_POST['username'] - &gt;不同UserName!= username
  • "中的未转义双引号UserName&amp; password输入 - &gt; <input type="text" ...应为<input type=\"text\" ...<input type='text' ...
  • 您错过了";之后和</form>之前的关闭} else - &gt; </form> }else{应为</form>"; }else{

尝试将您的PHP代码更新为

<?php

    session_start();

    if(isset($_POST['UserName'])){ 

        if(($_POST['UserName'] == "admin") && ($_POST['password'] == "adminpass"))
        {
            $_SESSION['secured'] = "Secured";
        }
        else
        {
            echo "Oops! Looks like the username and password is not what I'm looking for, sorry you can't continue <p><a href='?'>Lets try that again...</a>";
        }
    }


if(!isset($_SESSION['secured']))
{
    echo "<form method='post'>

    <form id='login' action='login.php' method='post' accept-charset='UTF-8'>
        <fieldset>
            <legend>Login</legend>
            <input type='hidden' name='submitted' id='submitted' value='1'/>

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

            <label for='password' >Password:</label>
            <input type='password' name='password' id='password' maxlength='50' required='true'/>

            <input type='submit' name='Submit' value='Submit' />    
        </fieldset>
    </form>";
}
else
{
    ?>


<html>
    <head>
        <title>Session Login</title>
    </head>
    <body>
        <p>You have been successfully logged in ... </p>
    </body>
</html>


<?php 
    }
?>