本文實例講述了php防止sql注入中過濾分頁參數的方法。分享給大家供大家參考。具體分析如下:
就網絡安全而言,在網絡上不要相信任何輸入信息,對於任何輸入信息我們都必須進行參數過濾。對此,我們先來看看下面的實例:
復制代碼 代碼如下:$this->load->library ( 'pagination' );
$config ['base_url'] = site_url () . '/guest/show';
$config ['total_rows'] = $c;
$config ['per_page'] = $pernum = 15;
$config ['uri_segment'] = 3;
$config ['use_page_numbers'] = TRUE;
$config ['first_link'] = '第一頁';
$config ['last_link'] = '最後一頁';
$config ['num_links'] = 5;
$this->pagination->initialize ( $config );
if (! $this->uri->segment ( 3 )) {
$currentnum = 0;
} else {
$currentnum = is_numeric($this->uri->segment ( 3 ))?(intval($this->uri->segment ( 3 ) - 1)) * $pernum:0;
}
$current_page=is_numeric($this->uri->segment ( 3 ))?intval($this->uri->segment ( 3 )):1;
if($current_page){
$data ['title'] = '第'.$current_page.'頁-留言本-防SQL注入測試';
}
else{
$data ['title'] = '留言本-防SQL注入測試';
}
$data ['liuyan'] = $this->ly->getLy ( $pernum, $currentnum );
其中:
復制代碼 代碼如下:$current_page=is_numeric($this->uri->segment ( 3 ))?intval($this->uri->segment ( 3 )):1;
$currentnum = is_numeric($this->uri->segment ( 3 ))?(intval($this->uri->segment ( 3 ) - 1)) * $pernum;
這兩句判斷了參數是否為數字。防止非法字符輸入。
希望本文所述對大家的PHP程序設計有所幫助。
我在PHP4環境下寫了一個防SQL注入的代碼,經過實際使用在PHP5下也兼容,歡迎大家使用修改,使用。
代碼如下:
<?php
/*
sqlin 防注入類
*/
class sqlin
{
//dowith_sql($value)
function dowith_sql($str)
{
$str = str_replace("and","",$str);
$str = str_replace("execute","",$str);
$str = str_replace("update","",$str);
$str = str_replace("count","",$str);
$str = str_replace("chr","",$str);
$str = str_replace("mid","",$str);
$str = str_replace("master","",$str);
$str = str_replace("truncate","",$str);
$str = str_replace("char","",$str);
$str = str_replace("declare","",$str);
$str = str_replace("select","",$str);
$str = str_replace("create","",$str);
$str = str_replace("delete","",$str);
$str = str_replace("insert","",$str);
$str = str_replace("'","",$str);
$str = str_replace(""","",$str);
$str = str_replace(" ","",$str);
$str = str_replace("or","",$str);
$str = str_replace("=","",$str);
$str = str_replace("%20","",$str);
//echo $str;
return $str;
}
//aticle()防SQL注入函數
function sqlin()
{
foreach ($_GET as $key=>$value)
{
$_GE......余下全文>>
基本上我都是用htmlspecialchars()和mysql_escape_string()這兩個方法把獲取到的參數處理一下。。