欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php str_replace的替換漏洞

 更新時間:2008年03月15日 17:30:39   作者:  
php 的函數(shù)str_replace替換漏洞
定義和用法
str_replace() 函數(shù)使用一個字符串替換字符串中的另一些字符。

語法
str_replace(find,replace,string,count)

參數(shù) 描述
find 必需。規(guī)定要查找的值。
replace 必需。規(guī)定替換 find 中的值的值。
string 必需。規(guī)定被搜索的字符串。
count 可選。一個變量,對替換數(shù)進行計數(shù)。

提示和注釋
注釋:該函數(shù)對大小寫敏感。請使用 str_ireplace() 執(zhí)行對大小寫不敏感的搜索。

注釋:該函數(shù)是二進制安全的。

例子 1
復制代碼 代碼如下:

<?php
echo str_replace("world","John","Hello world!");
?>

輸出:

Hello John!

例子 2
在本例中,我們將演示帶有數(shù)組和 count 變量的 str_replace() 函數(shù):
復制代碼 代碼如下:

<?php
$arr = array("blue","red","green","yellow");
print_r(str_replace("red","pink",$arr,$i));
echo "Replacements: $i";
?>

輸出:
Array
(
[0] => blue
[1] => pink
[2] => green
[3] => yellow
)
Replacements: 1

例子 3
復制代碼 代碼如下:

<?php
$find = array("Hello","world");
$replace = array("B");
$arr = array("Hello","world","!");
print_r(str_replace($find,$replace,$arr));
?>

輸出:

Array
(
[0] => B
[1] =>
[2] => !
)


漏洞相關函數(shù):

<?php

$arr1 = Array( 
'http://img.jb51.net/img/offer/29/24/70/20/29247020',
'http://img.jb51.net/img/offer/29/24/70/20/29247020-1',
'http://img.jb51.net/img/offer/29/24/70/20/29247020-2'
);
$arr2 = Array(
'http://localhost/root/ups/af48056fc4.jpg',
'http://localhost/root/ups/cf33240aa3.jpg',
'http://localhost/root/ups/c30e40419b.jpg'
);
$data = '
<img src="http://img.jb51.net/img/offer/29/24/70/20/29247020"/>
<img src="http://img.jb51.net/img/offer/29/24/70/20/29247020-1"/>
<img src="http://img.jb51.net/img/offer/29/24/70/20/29247020-2"/>';
$data = str_replace($arr1,$arr2,$data);
var_dump($data);
?>

替換后的結果是:


string(169) "<img src="http://localhost/root/ups/af48056fc4.jpg"/><img src="http://localhost/root/ups/af48056fc4.jpg-1"/><img src="http://localhost/root/ups/af48056fc4.jpg-2"/>"str_replace 函數(shù)的聲明大概是這樣: str_replace($search, $replace, $input[,&$count]), 比如在對一個字符串進行替換操作, $input 就是源字符串(稱為數(shù)據(jù)源). 這很不合理,因為它把數(shù)據(jù)源放在第3位, 而 str_pos, strtok, str_repeat 等等函數(shù)都是把數(shù)據(jù)源放在第1位.也就是說str_replace并沒有替換掉數(shù)組中相對應的字符串,而是把數(shù)組中的第一個替換,然后把相同的字符串后多余的合并。

解決辦法:
function strrplace($arr1,$arr2,$data){ 
if(is_array($arr1)) {  
foreach($arr1 as $key => $value)  {
   $data = str_replace_once($value, $arr2[$key], $data);
  } } 
return $data;
}
function str_replace_once($needle, $replace, $data) //替換第一次
{
$pos = strpos($data, $needle);
if ($pos === false) {
return $data; 
}
return substr_replace($data, $replace, $pos, strlen($needle));

相關文章

最新評論