2012-09-03 39 views
0

我有以下的html頁面: 我想要做的是大寫第一個字母(這是工作沒有任何問題),並剝離任何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 
} 
?> 
+0

只能首字母大寫或每個單詞的第一個字母? – VisioN

+0

它的名字,所以我只是首字母大寫。 – Interfaith

+0

我在代碼中看不到任何jQuery,是否有可能選擇的標記不正確? –

回答

1

加入我希望newValue = newValue.replace(/(<([^>]+)>)/ig,"");form.value = newValue;

+0

OriginalString只是一個示例變量。我應該用NewValue替換嗎? – Interfaith

+0

對不起,是的。編輯。 –

+0

我添加了它,它只是大寫,但並不刪除「<」作爲測試 – Interfaith

1

你意識到,任何惡意用戶可以只完全繞過你的函數。我所要做的就是將capitalize=function(){}放在我的控制檯中,突然間我可以寫出我想要的東西......或者我可以禁用JavaScript。無論哪種方式,我可以發送我喜歡的任何東西到您的服務器

在服務器端,你應該使用像PHP的htmlspecialchars的功能,以防止HTML被注入到用戶的瀏覽器,而你在它,你可以使用ucfirst大寫首字母(或ucwords爲先每個字母的字母)。

+0

我會發布頁面,所以你可以根據我的建議進行推薦。它不會向服務器發送任何信息。但讓我更新內容並讓我知道。謝謝 – Interfaith

+0

'

'......它將數據發送到服務器。 –

+0

更新了我的問題... – Interfaith

相關問題