php登录表单,包含消息、日期和时间



我正试图在表单中创建一个php日志。我只想做一些调整,但当我修改它时,它就停止工作了。。。

如果您无法从代码中看到,我将尝试创建一个(模拟)登录表单,要求提供用户名和密码。

  • 我希望任何空白的文本框都在文本框的右侧显示一条红色消息。(我有红色错误信息,但我不能把它放在盒子的左边)

  • 我想要一个粘性表格,如果填写了任何一个字段,它都会保持(再次,我想我已经设置好了,但不认为它一直有效)

  • 我希望输入用户名user和密码abc123的人看到欢迎信息。如果你不使用用户名/密码组合,我想要一条消息,说他们没有被授权。(这是我真的不知道该怎么做)

  • 我想把这一切都放在一个redux中(也认为我有工作,但不是100%确定)

任何帮助都将不胜感激!!

这是我的代码:

<?php
define('TITLE', 'LOG IN');
// CSS
print '<style type="text/css" media="screen">
.error { color: red; }
    </style>';
    // Checking
    if ( isset($_POST['submitted']) ) {
$problem = FALSE;
// Each value
if (empty($_POST['email'])) {
    $problem = TRUE;
    print '<p class="error">Please enter the username!</p>';
}
if (empty($_POST['password1'])) {
    $problem = TRUE;
    print '<p class="error">Please enter the password!</p>';
}

if (!$problem) { //No problem
    // Printing the log in message
    print '<p>Thank you for logging in!</p>';
    $_POST = array();
} else {
    print '<p class="error">No entry!</p>';
       }
    }
    ?>
    <form action="login.php" method="post">
    <p>"Username": <input type="text" name="username" size="20" value="<?php if (isset($_POST['username'])) { print htmlspecialchars($_POST['username']); } ?>" /></p>
    <p>Password: <input type="password" name="password1" size="20" /></p>
    <p><input type="submit" name="submit" value="Log in" /></p>
    <input type="hidden" name="submitted" value="true" />
    </form>

好的,这里有一个简单的登录,不适合在现实世界中使用。请阅读代码中的注释,看看我对每一条都有什么看法。由于多种原因,登录非常棘手,因此本示例并不旨在演示真实世界中可工作的代码库,而是一个非常简单的用户名/密码检查。

与更复杂的使用相关的安全问题可能超出了这个答案的范围,但下面的代码是我解释您在上面发布的内容的方式,而没有详细说明(可能会让人很难理解发生的最简单的步骤)。

如果你有任何问题,请告诉我。要查看正在运行的表单,请检查:

http://jfcoder.com/test/simplelogin.php

此外,为了简单起见,我使用了PHP的HEREDOC语法,而不是带引号的字符串。要阅读更多关于这个有时很方便的表格,请参阅

http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc.

<html>
<head>
<style type="text/css">
.error {
    color: red;
}
</style>
</head>
<body>
<?php 
// Note, in most cases you will set a SESSION variable
// of $_SESSION['loggedin'], which would require you to
// use session_start() before you access any session
// variables.
// Note, this defaults to false.
$loggedin = false;
// If I get an error, I will put it in this variable.
$error = '';
// If the username is provided, run the code. Otherwise,
// act as if the login form was not submitted. This makes
// a hidden `submitted` value superfluous, and guarantees
// your users at least provide a username.
if ($_POST['username']) {
    // NOTE!!! In mose cases, you're querying a database
    // for a username/password match. In PHP, this often 
    // means a MySQL query. DO NOT USE THE BELOW IF YOU
    // ARE DOING SO!!! This will allow what's called a
    // SQL injection. You MUST wash your data with something
    // like mysql_real_escape_string() for the $_POST
    // values (NEVER trust submitted data, always validate
    // and escape as necessary), or use the PHP PDO library.
    // In this example, though, I use a switch to check the
    // values for exact matches, which means I do not need 
    // to escape (and mysql_real_escape_string() requires
    // a database connection to use).
    $username = $_POST['username'];
    $password = $_POST['password'];
    // Here, I check if the username and password match.
    // This is, of course, hardcoded, but to match your 
    // attempt, I chose to keep the form, although you
    // rarely see this in use in the real world.    
    switch ($username) {
        // My one case. For each additional user, you
        // would need to add a new entry with password
        // check. And I set my error text according to
        // the result of the code.
        case 'user':
            if ($password === 'abc123') {
                $loggedin = true;
            } else {
                $error = 'Username/Password did not match.';
            }
            break;
        default:
            // Note, I don't give a descriptive error
            // here. If someone reports this error, I
            // know what may have gone wrong, but the
            // user is not told the username does not
            // exist.
            $error = 'Unknown error. Try again.';
    }
}
// I will only show the welcome message if the user has
// successfully logged in.
if ($loggedin === true) {
    echo <<<HTML
<h1>Welcome!</h1>
<p>Thank you for logging in $username</p>
HTML;
} else {
    // If an error text is set, display that error.
    if ($error != '') {
        $error = "<h4>Login error</h4><p class='error'>$error</p>";
    }
    // Here's my form, only shown if the user has not
    // successfully logged in (note, this is only a one-
    // time check when the POST data is submitted; I
    // would need to use sessions to "remember" the requestor
    // had logged in across page accesses.
    echo <<<FORM
<h1>Login Form</h1>
<form action="simplelogin.php" method="POST">
$error
<p><label>Username: <input type="text" name="username"/></label></p>
<p><label>Password: <input type="password" name="password"/></label></p>
<p><input type="submit" value="Login"/> <input type="reset"/></p>
</form>
FORM;
}
?>
</body>
</html>

这是我的完整代码。我几乎重写了整件事,所以如果编码风格差异太大,我会道歉:

<?php
// Output our CSS code
echo    '<style type="text/css" media="screen">
            .error
            {
                color: red;
            }
        </style>';
// Define our variable      
$problem = false;
// Check if the form has been submitted
if (isset($_POST['submitted']))
{
    // If either user or password are empty, we have a problem
    if (empty($_POST['username']) || empty($_POST['password']))
    {
        $problem = TRUE;
    }
    // If there is no problem, username is user, and password is abc123, we're good
    if (!$problem && $_POST['username']=='user' && $_POST['password']=='abc123') {
        // Print our login message
        echo 'Thank you for logging in!<br />';
    }
    // Ok, there's either a problem or the username or password is wrong, so no entry for them
    else
    {
        echo '<p class="error">No entry!</p>';
    }
}
    ?>
    <form action="login.php" method="post">
    Username: <input type="text" name="username" size="20" value="<?php
        if (isset($_POST['submitted']) && !empty($_POST['username']))
        {
            echo $_POST['username'];
        } ?>" />
    <?php
        if (isset($_POST['submitted']) && empty($_POST['username']))
        {
            echo '<span class="error">Please enter a username!</span>';
        }
    ?>
    <br />Password: <input type="password" name="password" size="20" value="<?php
        if (isset($_POST['submitted']) && !empty($_POST['password']))
        {
            echo $_POST['password'];
        } ?>" />
    <?php
        if (isset($_POST['submitted']) && empty($_POST['password']))
        {
            echo '<span class="error">Please enter the password!</span>';
        }
    ?>
    <br /><input type="submit" value="Log in" />
    <br /><input type="hidden" name="submitted" value="true" />
</form>

相关内容

  • 没有找到相关文章

最新更新