php - 将第一个字母大写并去掉 html 代码

标签 php javascript

我有以下 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;

关于php - 将第一个字母大写并去掉 html 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12252832/

相关文章:

javascript - 在 beforeunload/unload 上发送 Ajax 调用

javascript - .remove() 不是 Photoshop CC 2017 ExtendScript 工具包中的函数错误

javascript - 将 PHP 应用程序编译为具有变体的 HTML

php - Zend DB 表元数据对于整数列长度返回 null

PHP 简化一个三元运算

javascript - 如何在 Winston 中将错误对象记录到文件而不是控制台?

javascript - 为什么我可以让 console.log(variable) 给出我的值但不能返回变量?

php - 选择...不存在的地方

php - 以编程方式根据 Salesforce 检查 Enterprise WSDL

javascript - react : Avoid re-rendering of Component on state change