繁体   English   中英

首字母大写和剥离html代码

[英]capitalize first letter and strip html code

我有以下html页面:我要做的是将第一个字母大写(没有任何问题)并删除任何html代码的文本框或任何具有“<”“/>”的组合的代码>“因此用户无法插入恶意代码以在服务器端执行。

我发现了一个剥离的代码:

var StrippedString = OriginalString.replace(/(<([^>]+)>)/ig,"");

如何将上面的代码添加到下面的页面,以便它们同时处理?

<?php
/**
 * ****************************************************************************
 * Micro Protector
 * 
 * Version: 1.0
 * Release date: 2007-09-10
 * 
 * USAGE:
 *   Define your requested password below and inset the following code
 *   at the beginning of your page:
 *   <?php require_once("microProtector.php"); ?>
 * 
 *  
 * 
 ******************************************************************************/


$Password = 'TESTPASS'; // Set your password here



/******************************************************************************/
   if (isset($_POST['submit_pwd'])){
      $pass = isset($_POST['passwd']) ? $_POST['passwd'] : '';

      if ($pass != $Password) {
         showForm("Wrong password");
         exit();     
      }
   } else {
      showForm();
      exit();
   }

function showForm($error="LOGIN"){
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html>
<head>
<META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
   <title>IMC - Authentication</title>
   <link href="style/style.css" rel="stylesheet" type="text/css" />
<Script>
<!--
function capitalize(form) {
    value = form.value;
    newValue = '';
    value = value.split(' ');
    for(var i = 0; i < value.length; i++) {
        newValue += value[i].substring(0,1).toUpperCase() +
        value[i].substring(1,value[i].length) + '';
    }
form.value = newValue;
}
-->
</Script>
</head>
<body>
<center><a href="http://www.test.com"><img src="test.png" border=0 /></a></center>
<br><br><br>
    <div id="main">
      <div class="caption"><?php echo $error; ?></div>
      <div id="icon">&nbsp;</div>
      <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="pwd">
    Your Name:
        <table>
          <tr><td><input class="text" name="name" onBlur="capitalize(this);" maxlength=12 type="text" /></td></tr>
        </table> 
        Password:
        <table>
          <tr><td><input class="text" name="passwd" maxlength=8 type="password" /></td></tr>
          <tr><td align="center"><br/>
             <input class="text" type="submit" name="submit_pwd" value="Login" />
          </td></tr>
        </table>  
      </form>
   </div>
</body>
</html>

<?php   
}
?>

添加newValue = newValue.replace(/(<([^>]+)>)/ig,""); form.value = newValue;之前form.value = newValue;

我希望你意识到任何恶意用户都可以完全绕过你的功能。 我所要做的就是在我的控制台中放置capitalize=function(){} ,突然间我可以写出我想要的东西......或者我可以禁用JavaScript。 无论哪种方式,我都可以向服务器发送任何我喜欢的内容。

在服务器端,你应该使用像PHP的htmlspecialchars这样的函数来防止HTML被注入到用户的浏览器中,当你在它的时候,你可以使用ucfirst来大写第一个字母(或者每个单词的第一个字母的ucwords )。

暂无
暂无

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

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