输入过滤、自动换行和数据加密

今天在做tutu论坛的时候遇到了三个比较常见的问题。
1.输入过滤
    为了防止单引号截断sql语句或sql注入,以及防止用户编辑时使用的脚本符号被意外转义,需要对用户输入进行过滤。过滤函数如下:
    (C#)
    string Encode(string strInput)
    {
     string strResult;
     strResult = strInput.Replace("&","&");
     strResult = strResult.Replace("<","&#060;");
     strResult = strResult.Replace("\'","&#039;");
     strResult = strResult.Replace(">","&#062;");
     strResult = strResult.Replace(" ","&nbsp;");
     strResult = strResult.Replace("\r\n","<br/>");
     return strResult; 
    }
    其中"\r\n"代表编辑时输入的回车换行符,它要被转化为<br/>才能在网页中换行。
2.英文的自动换行
    即使在表格的宽度指定的情况下,表格数据是英文或数字时,仍会发生数据撑大表格的情况,为此,可以用如下方法:
    <Table style="TABLE-LAYOUT: fixed" ....>
        <tr>
                 <td style="WORD-WRAP: break-word">
                        ...........
                  </td>
        </tr>
      </Table>
3.数据加密
    用户的密码要保存在数据库中时,最好把密码加密后保存,.net中自带有加密算法:
    Namespace:System.Web.Security
    public static string HashPasswordForStoringInConfigFile( string password, string passwordFormat);
    其中passwordFormat可选为"sha1"或"md5"


posted @ 2005-04-12 02:56  刀刀  阅读(571)  评论(0)    收藏  举报