我在尝试开发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上进行了测试,并且问题在布鲁塞尔中保持一致。
答案 0 :(得分: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脚本的开头设置需要评论