【问题标题】:HTML displayed as textHTML 显示为文本
【发布时间】:2020-08-10 09:05:15
【问题描述】:

我有一个包含 html 代码的 .php 文件。我遇到的问题是它显示为文本,而不是 html 元素。我发现了关于同一问题的其他几个问题:

Why is this HTML showing up as plain text in browser?

Browser shows plain text instead of HTML in mac

Page shows code not rendering

但是,我相信人们在那里提出的所有建议都适用于我的代码。

我的代码:

<?php
// required headers
header("Access-Control-Allow-Origin: null");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

// files needed to connect to database
include_once '../config/database.php';

// get database connection
$database = new Database();
$conn = $database->getConnection();

$error = "";
if (isset($_GET["key"]) && isset($_GET["email"]) && isset($_GET["action"]) && ($_GET["action"]=="reset") && !isset($_POST["action"])) {
    $key = $_GET["key"];
    $email = $_GET["email"];
    $curDate = date("Y-m-d H:i:s");

    $query = "SELECT * FROM password_reset_temp WHERE email=:email AND `key`=:key";
    $stmt = $conn->prepare($query);
    $stmt->bindParam(":key", $key);
    $stmt->bindParam(":email", $email);
    try {
        $stmt->execute();
    } catch (PDOException $e) {
        echo json_encode(array("message" => $e->getMessage()));
    }
    if ($stmt->rowCount() == 0) {
        $error .= '<h2>Invalid Link</h2>
        <p>The link is invalid/expired. Either you did not copy the correct link
        from the email, or you have already used the key in which case it is 
        deactivated.</p>
        <p><a href="...">
        Click here</a> to reset password.</p>';
    } else {
        $row = $stmt->fetch();
        $expDate = $row['expDate'];
        if ($expDate >= $curDate){
// the part that is not showing begins here                
?>
            <br />
            <form method="post" action="" name="update">
            <input type="hidden" name="action" value="update" />
            <br /><br />
            <label><strong>Enter New Password:</strong></label><br />
            <input type="password" name="pass1" maxlength="15" required />
            <br /><br />
            <label><strong>Re-Enter New Password:</strong></label><br />
            <input type="password" name="pass2" maxlength="15" required/>
            <br /><br />
            <input type="hidden" name="email" value="<?php echo $email;?>"/>
            <input type="submit" value="Reset Password" />
            </form>
            <?php
        } else {
            $error .= "<h2>Link Expired</h2>
            <p>The link is expired. You are trying to use the expired link which 
            as valid only 24 hours (1 days after request).<br /><br /></p>";
        }
    }
    if($error!=""){
        echo "<div class='error'>".$error."</div><br />";
    } 
} // isset email key validate end


if(isset($_POST["email"]) && isset($_POST["action"]) &&
 ($_POST["action"]=="update")){
$error="";
$pass1 = mysqli_real_escape_string($con,$_POST["pass1"]);
$pass2 = mysqli_real_escape_string($con,$_POST["pass2"]);
$email = $_POST["email"];
$curDate = date("Y-m-d H:i:s");
if ($pass1!=$pass2){
$error.= "<p>Password do not match, both password should be same.<br /><br /></p>";
  }
  if($error!=""){
echo "<div class='error'>".$error."</div><br />";
}else{
$pass1 = md5($pass1);
mysqli_query($con,
"UPDATE `users` SET `password`='".$pass1."', `trn_date`='".$curDate."' 
WHERE `email`='".$email."';"
);

mysqli_query($con,"DELETE FROM `password_reset_temp` WHERE `email`='".$email."';");

echo '<div class="error"><p>Congratulations! Your password has been updated successfully.</p>
<p><a href="...">
Click here</a> to Login.</p></div><br />';
   } 
}
?>

我的 .htaccess 看起来像这样

# Turn on the rewrite engine
RewriteEngine  on
# If the request doesn't end in .php (Case insensitive) continue processing rules
RewriteCond %{REQUEST_URI} !\.php$ [NC]
# If the request doesn't end in a slash continue processing the rules
RewriteCond %{REQUEST_URI} [^/]$
# Rewrite the request with a .php extension. L means this is the 'Last' rule
RewriteRule ^(.*)$ $1.php [L]

输出:

<br />
            <form method="post" action="" name="update">
            <input type="hidden" name="action" value="update" />
            <br /><br />
            <label><strong>Enter New Password:</strong></label><br />
            <input type="password" name="pass1" maxlength="15" required />
            <br /><br />
            <label><strong>Re-Enter New Password:</strong></label><br />
            <input type="password" name="pass2" maxlength="15" required/>
            <br /><br />
            <input type="hidden" name="email" value="..."/>
            <input type="submit" value="Reset Password" />
            </form>

该目录中的所有其他 .php 文件都可以正常工作。如果您需要任何其他信息,请告诉我。

【问题讨论】:

  • 这是嵌入在另一个文件中吗?如果没有,您至少缺少&lt;html&gt;&lt;body&gt; 标签。
  • 你应该learn how to use the label element properly。如果没有 for 属性或其中的表单控件,标签是无用的。 (如果你要在非常标签内放置一个&lt;string&gt;,你可能应该在它们上使用font-weight
  • 多个 &lt;br&gt; 元素不能很好地替代 CSS 边距。
  • 危险:你容易受到SQL injection attacks的影响,你需要defend你自己。
  • 危险:您使用的是an unsuitable hashing algorithm,需要take better care的用户密码。

标签: php html


【解决方案1】:
header("Content-Type: application/json; charset=UTF-8");

你说它是 JSON 而不是 HTML。

浏览器试图将其视为 JSON,但失败,并将其显示为文本。

【讨论】:

  • 这确实导致了错误,将它从“application/json”更改为“html”对我有用!谢谢
  • "html" 不是有效的内容类型。它应该是text/html,但这是 PHP 的默认设置,因此请完全删除它。
猜你喜欢
  • 1970-01-01
  • 2013-05-19
  • 2019-03-14
  • 2015-07-06
  • 2022-01-21
  • 2017-09-14
  • 2017-11-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多