将第一个字母大写并去掉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"> </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
}
?>
在form.value = newValue;
之前添加newValue = newValue.replace(/(<([^>]+)>)/ig,"");
我希望您意识到,任何恶意用户都可以完全绕过您的功能。我所要做的就是把capitalize=function(){}
放在我的控制台上,突然间我可以写我想要的。。。或者我可以禁用JavaScript。无论哪种方式,我都可以将我喜欢的任何内容发送到您的服务器。
在服务器端,您应该使用像PHP的htmlspecialchars
这样的函数来防止HTML被注入到用户的浏览器中,并且在使用时,您可以使用ucfirst
来大写第一个字母(或者ucwords
表示每个单词的第一个字母)。
相关文章:
- 如何解析结构不良的 html 代码
- 我的html表单无法验证.请参阅代码片段中的html代码和java脚本
- 将第一个字母大写并去掉html代码
- 我在下拉列表中尝试了下一个和前五年的html代码.接下来的5年我都过得很好.我怎样才能拿到之前的5年
- 使用.format在一些HTML代码中插入文本,会出现错误(Python)
- html代码没有在记事本++上运行
- 来自mysql的动态值用于html代码点火器视图中的图形
- 如何:编写漂亮的HTML代码和Javascript控制台
- 我需要java代码来打开html代码onclick事件
- html代码需要可点击的文本按钮
- 是否可以在JavaScript中包含HTML代码
- 如果javascript打开/关闭,则隐藏和显示html代码
- 使用PartialView作为带有变量的可重悬浮html代码
- 在jquery中复制特定的html代码
- 如何在AngularJS的上下文中使用Google Chrome Developer Tool在HTML代码中设置断点
- 显示HTML代码块
- 从HTML代码中提取数据
- 如何获取保存在变量中的HTML代码的任何属性的值
- 将html代码转换为十六进制
- JavaScript和Html代码来运行所有类型的视频并获取状态