【问题标题】:Bizarre HTML Behaviour奇怪的 HTML 行为
【发布时间】:2015-10-30 04:25:06
【问题描述】:

我在尝试为我的 Intranet 数据库开发登录屏幕时遇到了一个奇怪的问题。当我尝试使用正确的用户名和密码配对登录时,它会失败,除非我从页面代码中添加或删除一些(看似无关的)HTML行和/或更改页面代码中一行的缩进和/或移动一个元素的标签到或来自同一行。对于需要进行哪些修改,我无法辨别韵律或原因。

我已包含以下代码来说明问题。请注意:为了极简主义,我已尝试减少此代码,但如果我进一步减少它,则不会出现问题。

HTML / PHP (Index.php)

<!DOCTYPE html>

<?php
    require "ConnDB.php";
?>

<html>
    <head>
        <meta charset = "utf-8">

        <meta name    = "viewport"
              content = "initial-scale = 1.0, width = device-width">

        <link href = "CSS/Intranet%20Project.css"
              rel  = "stylesheet"
              type = "text/css">
    </head>

    <body>
        <form id     = "entire-page"
              action = "Index.php"
              method = "post"
        >
            User Name

            <input id    = "user-name-entry"
                   class = "border-01px-black-solid"
                   name  = "user-name-value"
                   type  = "text"
            >

            Password

            <input id    = "password-entry"
                   class = "border-01px-black-solid"
                   name  = "password-value"
                   type  = "password"
            >

            <input id    = "login-button"
                   name  = "login-submit"
                   type  = "submit"
                   value = "Log In"
            >

            <div id = "footer-section">
                <div id = "footer-internal-links">
                    <div id    = "footer-internal-links-box"
                         class = "vertically-centred"
                    >
                        <span id    = "footer-internal-links-site-map-text"
                              class = "footer-text
                                       footer-internal-links-text"
                        >
                            Site Map
                        </span>

                        <div id    = "footer-internal-links-buffer-1"
                             class = "footer-internal-links-horizontal-buffer-015px"
                        >
                        </div>

                        <span id    = "footer-internal-links-privacy-text"
                              class = "footer-text
                                       footer-internal-links-text"
                        >
                            Privacy
                        </span>

                        <div id    = "footer-internal-links-buffer-2"
                             class = "footer-internal-links-horizontal-buffer-015px"
                        >
                        </div>

                        <span id    = "footer-internal-links-disclaimer-text"
                              class = "footer-text
                                       footer-internal-links-text"
                        >
                            Disclaimer
                        </span>
                    </div>
                </div>

                <div id = "footer-external-links">
                    <div id    = "footer-external-links-box"
                         class = "vertically-centred"
                    >
                        <img id    = "footer-external-link-facebook"
                             alt   = "External Link Facebook"
                             src   = "Images/Footer%20Facebook%20Symbol%2032%20x%2032.png"
                        >

                        <div id    = "footer-external-links-buffer-1"
                             class = "footer-external-links-horizontal-buffer-015px"
                        >
                        </div>

                        <img id    = "footer-external-link-twitter"
                             alt   = "External Link Twitter"
                             src   = "Images/Footer%20Facebook%20Symbol%2032%20x%2032.png"
                        >

                        <div id    = "footer-external-links-buffer-2"
                             class = "footer-external-links-horizontal-buffer-015px"
                        >
                        </div>

                        <img id    = "footer-external-link-youtube"
                             src   = "Images/Footer%20Facebook%20Symbol%2032%20x%2032.png"
                        >
                    </div>
                </div>
            </div>
        </form>

        <?php
            if ( isset( $_POST[ 'login-submit' ] ) )
            {
                $userName = trim( $_POST[ 'user-name-value' ] );
                $password = trim( $_POST[ 'password-value' ] );

                $intranetDatabaseConnection = $dbConnection->prepare( "SELECT * FROM tblAccount WHERE fldUserName = ? AND BINARY fldPassword = ?;" );

                if ( $intranetDatabaseConnection === false )
                {
                    echo '<script>alert( "Invalid Database connection" )</script>';
                }
                else
                {
                    $intranetDatabaseConnection->bindParam( 1, $userName );
                    $intranetDatabaseConnection->bindParam( 2, $password );
                    $intranetDatabaseConnection->execute();

                    if ( $intranetDatabaseConnection->rowCount() == 0 )
                    {
                        echo '<script>alert("An invalid combination of User Name and Password was entered.")</script>';
                    }
                    else
                    {
                        $_SESSION[ "InternalSummons" ] = true;

                        foreach ( $intranetDatabaseConnection as $intranetDatabaseRow )
                        {
                            if ( $intranetDatabaseRow['fldLevel'] == 1 )
                            {
                                $_SESSION[ "UserLevel" ] = "Administrator";

                                $host = $_SERVER[ "HTTP_HOST" ];
                                $uri  = rtrim( dirname( $_SERVER[ "PHP_SELF" ] ),
                                               '/\\' );

                                $extra = "User%20Administration.php";

                                header( "Location: http://$host$uri/$extra" );
                            }
                            else
                            {
                                $_SESSION[ 'UserLevel' ] = "Invalid";

                                echo "<script>alert( \"An invalid User Access Level has been detected for that User.\" )</script>";
                            }
                        }
                    }
                }
            }
        ?>
    </body>
</html>

HTML / PHP (ConnDB.php)

<?php
    session_start();

    try
    {
        $dbConnection = new PDO( "mysql:host=localhost; dbname=intranet_project; charset=utf8",
                                 "root",
                                 "Password1" );
    }
    catch ( PDOException $exception )
    {
        echo "Failure : " . $exception->getMessage();
        $dbConnection->rollBack();
    }

    $_SESSION[ "seed" ] = SHA1( "IllBeThereForYou" );

    $dbConnection->setAttribute( PDO::ATTR_EMULATE_PREPARES,
                                 false );

    date_default_timezone_set( "Australia/Perth" );
?>

CSS (Intranet Project.css)

html,
*
{
    border      : 0;
    box-sizing  : border-box;
    margin      : 0;
    padding     : 0;
}

.border-01px-black-solid
{
    border       : 1px;
    border-color : black;
    border-style : solid;
}

#footer-section
{
    background-color : cyan;
}

MySQL 示例数据库脚本

CREATE DATABASE Intranet_Project;

USE Intranet_Project;

CREATE TABLE tblAccount
(
    fldID         MEDIUMINT       NOT NULL AUTO_INCREMENT,
    fldUserName   VARCHAR( 20 )   NOT NULL UNIQUE,
    fldPassword   VARCHAR( 20 )   NOT NULL,
    fldLevel      TINYINT( 1 )    NOT NULL,
    PRIMARY KEY (fldID)
);

INSERT INTO tblAccount
SET fldUserName = "Sample User 001",
    fldPassword = "Password1",
    fldLevel    = 1;

在这种情况下,显示的代码似乎接受了登录值,但只是将表单清空并且不会继续进入用户管理页面。如果我只是从 Index.php 中删除以下行,它确实会继续...

                            Site Map

我不知道为什么这么简单的改变会产生这样的效果。在过去的几天里,我一直在编辑原始文件以确定这个问题发生的位置,但它似乎无处不在 - 引用的示例只是我能够实现的问题的最简单说明。

过去几天我也花了很多时间研究这个问题,但我尝试的许多搜索条件都没有提供任何证明远程有用的东西。

我的问题是:为什么如上所述删除Site Map 可以解决问题以及首先导致问题的原因是什么?

请注意:此代码已在不同版本的 Firefox、Chrome 和 Internet Explorer 上进行了测试,并且问题在各兄弟之间保持一致。

【问题讨论】:

  • 您没有收到“标头已发送”警告/错误吗?

标签: javascript php html mysql css


【解决方案1】:

一个简单的解决方案,你不应该在header()之前写输出,因为你已经输出了html然后改变位置,它是行不通的。

 header( "Location: http://$host$uri/$extra" );

重新编辑

<?php

  require "ConnDB.php";
  $message="";
            if ( isset( $_POST[ 'login-submit' ] ) )
            {
                $userName = trim( $_POST[ 'user-name-value' ] );
                $password = trim( $_POST[ 'password-value' ] );

                $intranetDatabaseConnection = $dbConnection->prepare( "SELECT * FROM tblAccount WHERE fldUserName = ? AND BINARY fldPassword = ?;" );

                if ( $intranetDatabaseConnection === false )
                {
                    $message= '<script>alert( "Invalid Database connection" )</script>';
                }
                else
                {
                    $intranetDatabaseConnection->bindParam( 1, $userName );
                    $intranetDatabaseConnection->bindParam( 2, $password );
                    $intranetDatabaseConnection->execute();

                    if ( $intranetDatabaseConnection->rowCount() == 0 )
                    {
                        $message= '<script>alert("An invalid combination of User Name and Password was entered.")</script>';
                    }
                    else
                    {
                        $_SESSION[ "InternalSummons" ] = true;

                        foreach ( $intranetDatabaseConnection as $intranetDatabaseRow )
                        {
                            if ( $intranetDatabaseRow['fldLevel'] == 1 )
                            {
                                $_SESSION[ "UserLevel" ] = "Administrator";

                                $host = $_SERVER[ "HTTP_HOST" ];
                                $uri  = rtrim( dirname( $_SERVER[ "PHP_SELF" ] ),
                                               '/\\' );

                                $extra = "User%20Administration.php";

                                header( "Location: http://$host$uri/$extra" );
                            }
                            else
                            {
                                $_SESSION[ 'UserLevel' ] = "Invalid";

                                $message= "<script>alert( \"An invalid User Access Level has been detected for that User.\" )</script>";
                            }
                        }
                    }
                }
            }
        ?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset = "utf-8">

        <meta name    = "viewport"
              content = "initial-scale = 1.0, width = device-width">

        <link href = "CSS/Intranet%20Project.css"
              rel  = "stylesheet"
              type = "text/css">
    </head>

    <body>
        <form id     = "entire-page"
              action = "Index.php"
              method = "post"
        >
            User Name

            <input id    = "user-name-entry"
                   class = "border-01px-black-solid"
                   name  = "user-name-value"
                   type  = "text"
            >

            Password

            <input id    = "password-entry"
                   class = "border-01px-black-solid"
                   name  = "password-value"
                   type  = "password"
            >

            <input id    = "login-button"
                   name  = "login-submit"
                   type  = "submit"
                   value = "Log In"
            >

            <div id = "footer-section">
                <div id = "footer-internal-links">
                    <div id    = "footer-internal-links-box"
                         class = "vertically-centred"
                    >
                        <span id    = "footer-internal-links-site-map-text"
                              class = "footer-text
                                       footer-internal-links-text"
                        >
                            Site Map
                        </span>

                        <div id    = "footer-internal-links-buffer-1"
                             class = "footer-internal-links-horizontal-buffer-015px"
                        >
                        </div>

                        <span id    = "footer-internal-links-privacy-text"
                              class = "footer-text
                                       footer-internal-links-text"
                        >
                            Privacy
                        </span>

                        <div id    = "footer-internal-links-buffer-2"
                             class = "footer-internal-links-horizontal-buffer-015px"
                        >
                        </div>

                        <span id    = "footer-internal-links-disclaimer-text"
                              class = "footer-text
                                       footer-internal-links-text"
                        >
                            Disclaimer
                        </span>
                    </div>
                </div>

                <div id = "footer-external-links">
                    <div id    = "footer-external-links-box"
                         class = "vertically-centred"
                    >
                        <img id    = "footer-external-link-facebook"
                             alt   = "External Link Facebook"
                             src   = "Images/Footer%20Facebook%20Symbol%2032%20x%2032.png"
                        >

                        <div id    = "footer-external-links-buffer-1"
                             class = "footer-external-links-horizontal-buffer-015px"
                        >
                        </div>

                        <img id    = "footer-external-link-twitter"
                             alt   = "External Link Twitter"
                             src   = "Images/Footer%20Facebook%20Symbol%2032%20x%2032.png"
                        >

                        <div id    = "footer-external-links-buffer-2"
                             class = "footer-external-links-horizontal-buffer-015px"
                        >
                        </div>

                        <img id    = "footer-external-link-youtube"
                             src   = "Images/Footer%20Facebook%20Symbol%2032%20x%2032.png"
                        >
                    </div>
                </div>
            </div>
        </form>
    <?php

    echo $message;
?>

    </body>
</html>

重新编辑 标题应该在你的 php 脚本的开头设置需要注释

【讨论】:

  • 谢谢 Azmat karim - 在实施您将 PHP 控制代码移动到文件顶部的建议后,我的文件的示例和完整版本工作得很好。不过,还有两个后续问题。首先,您将错误消息更改为使用 $message 结构是否有特殊原因?其次,如果您删除了Site Map 行,为什么我的示例代码仍然有效?
  • 1-我使用了消息,因为您不想在页面顶部显示文本。其次, 标签外的文本对于 [w3c] (validator.w3.org) 标准无效。 2-它的奇怪真的不知道原因。好吧,您可以尝试将标签 用于文本站点地图、隐私和免责声明,并尝试这样做。让我知道这是否有效
猜你喜欢
  • 1970-01-01
  • 2019-02-16
  • 2021-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-25
  • 2020-11-14
相关资源
最近更新 更多