尝试使用html和php进行简单登录

问题描述 投票:0回答:2

我正在尝试使用硬编码的用户名和密码开发一个简单登录程序。但是,我的HTML代码显示用户名和密码框,它应该重定向到PHP文件,但它不会显示我的PHP文件输出上的任何内容。这是我的HTML代码:

<html>
<body>
    <form id='Login' action='homework2.php' method='POST' accept-charset='UTF-8'>
        <h1>Login</h1>
        <input type='hidden' name='submitted' id='submitted' value='1'/>
        <label for='username' >UserName:</label>
        <input type='text' name='username' id='username' maxlength="25" />
        <label for='password' >Password:</label>
        <input type='password' name='password' id='password' maxlength="25" />
        <input type='submit' name='Submit' value='Submit'/>
   </form>
</body>
</html>

这是我的php文件

<?php
function homework2() {
    if (empty($_POST['username'])) {
        $this->HandleError("UserName is empty!");
        return false;
    }
    if (empty($_POST['password'])) {
        $this->HandleError("Password is empty!");
        return false;
    }
    $username = trim($_POST['username']);
    $password = trim($_POST['password']);
    if (($username == 'elliez' && $password == 'tr789ial') ||($username == 'greatGuy' && $password == 'abc123') || ($username == 'blogger' && $password == '23seventeen23')) {
        echo "You have successfully logged in!";
        return true;
    }
    echo "Sorry, wrong information has been entered!";
    return false;
}
?>
php html
2个回答
1
投票

删除homework2()函数

<?php
if (empty($_POST['username']))
{
    die("UserName is empty!");
}
if (empty($_POST['password']))
{
    die("Password is empty!");
}
$username = trim($_POST['username']);
$password = trim($_POST['password']);
if (($username == 'elliez' && $password == 'tr789ial') ||
    ($username == 'greatGuy' && $password == 'abc123') ||
    ($username == 'blogger' && $password == '23seventeen23'))
{
    echo "You have successfully logged in!";
} else {
    die("Sorry, wrong information has been entered!");
}
?>

0
投票

您需要将php文件的名称设置为homework2.php,因为此表单的操作在homevork2.php页面上执行,并删除函数homevork2()。

© www.soinside.com 2019 - 2024. All rights reserved.