尝试为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的不同版本上进行了测试,并且问题在所有浏览器中均保持一致。
一个简单的解决方案,您不应该在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>
RE-EDIT标头应在您的php脚本开始处设置需要注释
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句