doudie2693 2010-05-04 20:21
浏览 31
已采纳

这些安全功能是否足够?

I am trying to secure my site so I'm not vulnerable to sql injection or xss.

Here's my code:

//here's the form (abbreviated)
<form>
<label for="first_name" class="styled">First Name:</label>
<input type="text" id="first_name" name="first_name" value="<?php if (!empty($first_name)) echo $first_name; ?>" /><br />

//submit button etc
</form>


if (isset($_POST['submit'])) {

 //gets rid of extra whitesapce and escapes
 $first_name = mysqli_real_escape_string($dbc, trim($_POST['first_name']));

 //check if $first_name is a string
 if(!is_string($first_name)
 { 
 echo "not string"; 
 }

 //then insert into the database. 
 .......

}

mysqli_real_escape_string: I know that this function escapes certain letters like , so when the data gets inputted into the dbc, would it have '\' next to all the escaped letters?

  • Will this script be enough to prevent most sql injection? just escaping and checking if the data is a string. For integers values(like users putting in prices), i just: is_numeric().

  • How should I use htmlspecialchars? Should I use it only when echoing and displaying user data? Or should I also use this when inserting data to a database?

  • When should I use strip_tags or htmlspecialchars?

So with all these functions:

if (isset($_POST['submit'])) {

 //gets rid of extra whitesapce and escapes
 $first_name = mysqli_real_escape_string($dbc, trim($_POST['first_name']));

 //check if $first_name is a string
 if(!is_string($first_name)
 { 
 echo "not string"; 
 }

 //gets rid of any <,>,&
 htmlspecialchars($first_name);

 //strips any tags with the first name
 strip_tags($first_name)

 //then insert into the database. 
 .......

}

Which functions should I use for sql injection and which ones should I use for xss?

When can a user insert xss scripts against me? When there is a form?

  • 写回答

4条回答 默认 最新

  • dongyi2425 2010-05-04 20:26
    关注

    For SQL injection, mysql_real_escape_string should work. (To be totally sure, you can use prepared statements)

    For XSS, htmlspecialchars should work. strip_tags might not be as safe, as someone could cleverly disguse their javascript.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源