繁体   English   中英

htmlentities()无法以php形式工作

[英]htmlentities() is not working in php form

htmlentities并没有崩溃,它应该转义引号,但事实并非如此。

<?php
session_start();
session_regenerate_id( true );
if(isset($_REQUEST['sub'])){
    echo $name = htmlspecialchars($_REQUEST['email'] );
    echo $pswd = $_REQUEST['pswd'];
    echo $abc = htmlentities($pswd ,ENT_QUOTES, "UTF-8");
    //echo $pswd = htmlentities("hello" ,ENT_QUOTES);   
}
?>


<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
  <h2>Login form</h2>
  <form action="" method="POST">
    <div class="form-group">
      <label for="email">Email:</label>
      <input type="email" class="form-control" id="email" placeholder="Enter email" name="email" required>
    </div>
    <div class="form-group">
      <label for="pwd">Password:</label>
      <input type="password" class="form-control" id="pwd" placeholder="Enter password" name="pswd" required>
    </div>    
    <button type="submit" class="btn btn-primary" name="sub">Submit</button>
  </form>
</div>

</body>
</html>

您可以看到1行是注释,如果我在htmlentites()中传递带引号的值,则输出不带引号的值,但是如果我从表单传递值,则假设我传递了“ hello”。 “ “ 被包含在内。 并将此值存储在php变量中,然后在htmlentites()中传递此变量。 它以“”显示输出,但应忽略引号。

两种htmlentities函数的使用方式有所不同。 如果您想模仿第一个的工作方式,则被注释掉的应该看起来像这样:

htmlentities('"hello"' ,ENT_QUOTES);

在注释掉的函数调用的示例中,引号用作函数的一部分,您传递了这样的参数,因此它们甚至都不会被函数解析。

htmlentities的目的不是剥离字符,而是将字符转义,因此您将永远不会看到缺少符号的情况。 例如,使用html实体可以在html中打印<p>类的字符串。 如果不通过htmlentities传递,它将呈现一个html p标签,如果通过htmlentities('<p>')之类的htmlentities('<p>')传递,它将显示该字符串。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM