[英]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
}
?>
添加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.