php str_replace的替換漏洞
更新時(shí)間:2008年03月15日 17:30:39 作者:
php 的函數(shù)str_replace替換漏洞
定義和用法
str_replace() 函數(shù)使用一個(gè)字符串替換字符串中的另一些字符。
語法
str_replace(find,replace,string,count)
參數(shù) 描述
find 必需。規(guī)定要查找的值。
replace 必需。規(guī)定替換 find 中的值的值。
string 必需。規(guī)定被搜索的字符串。
count 可選。一個(gè)變量,對(duì)替換數(shù)進(jìn)行計(jì)數(shù)。
提示和注釋
注釋:該函數(shù)對(duì)大小寫敏感。請(qǐng)使用 str_ireplace() 執(zhí)行對(duì)大小寫不敏感的搜索。
注釋:該函數(shù)是二進(jìn)制安全的。
例子 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] => !
)
漏洞相關(guān)函數(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);
?>
替換后的結(jié)果是:
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]), 比如在對(duì)一個(gè)字符串進(jìn)行替換操作, $input 就是源字符串(稱為數(shù)據(jù)源). 這很不合理,因?yàn)樗褦?shù)據(jù)源放在第3位, 而 str_pos, strtok, str_repeat 等等函數(shù)都是把數(shù)據(jù)源放在第1位.也就是說str_replace并沒有替換掉數(shù)組中相對(duì)應(yīng)的字符串,而是把數(shù)組中的第一個(gè)替換,然后把相同的字符串后多余的合并。
解決辦法:
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));
}
str_replace() 函數(shù)使用一個(gè)字符串替換字符串中的另一些字符。
語法
str_replace(find,replace,string,count)
參數(shù) 描述
find 必需。規(guī)定要查找的值。
replace 必需。規(guī)定替換 find 中的值的值。
string 必需。規(guī)定被搜索的字符串。
count 可選。一個(gè)變量,對(duì)替換數(shù)進(jìn)行計(jì)數(shù)。
提示和注釋
注釋:該函數(shù)對(duì)大小寫敏感。請(qǐng)使用 str_ireplace() 執(zhí)行對(duì)大小寫不敏感的搜索。
注釋:該函數(shù)是二進(jìn)制安全的。
例子 1
復(fù)制代碼 代碼如下:
<?php
echo str_replace("world","John","Hello world!");
?>
輸出:
Hello John!
例子 2
在本例中,我們將演示帶有數(shù)組和 count 變量的 str_replace() 函數(shù):
復(fù)制代碼 代碼如下:
<?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
復(fù)制代碼 代碼如下:
<?php
$find = array("Hello","world");
$replace = array("B");
$arr = array("Hello","world","!");
print_r(str_replace($find,$replace,$arr));
?>
輸出:
Array
(
[0] => B
[1] =>
[2] => !
)
漏洞相關(guān)函數(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);
?>
替換后的結(jié)果是:
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]), 比如在對(duì)一個(gè)字符串進(jìn)行替換操作, $input 就是源字符串(稱為數(shù)據(jù)源). 這很不合理,因?yàn)樗褦?shù)據(jù)源放在第3位, 而 str_pos, strtok, str_repeat 等等函數(shù)都是把數(shù)據(jù)源放在第1位.也就是說str_replace并沒有替換掉數(shù)組中相對(duì)應(yīng)的字符串,而是把數(shù)組中的第一個(gè)替換,然后把相同的字符串后多余的合并。
解決辦法:
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));
}
您可能感興趣的文章:
- PHP利用str_replace防注入的方法
- PHP的substr_replace將指定兩位置之間的字符替換為*號(hào)
- PHP中str_replace函數(shù)使用小結(jié)
- 詳解PHP字符串替換str_replace()函數(shù)四種用法
- php preg_match_all結(jié)合str_replace替換內(nèi)容中所有img
- str_replace只替換一次字符串的方法
- php使用str_replace實(shí)現(xiàn)輸入框回車替換br的方法
- php str_replace替換指定次數(shù)的方法詳解
- php 中的str_replace 函數(shù)總結(jié)
- 因str_replace導(dǎo)致的注入問題總結(jié)
相關(guān)文章
使用PHP提取視頻網(wǎng)站頁面中的FLASH地址的代碼
這幾天工作中需要寫個(gè)程序?qū)τ谝粋€(gè)視頻網(wǎng)站地址,如優(yōu)酷的某個(gè)地址,提取出其中的FLASH地址來。2010-04-04解析PHP的Yii框架中cookie和session功能的相關(guān)操作
這篇文章主要介紹了PHP的Yii框架中cookie和session功能的相關(guān)操作,需要的朋友可以參考下2016-03-03PHP仿tp實(shí)現(xiàn)mvc框架基本設(shè)計(jì)思路與實(shí)現(xiàn)方法分析
這篇文章主要介紹了PHP仿tp實(shí)現(xiàn)mvc框架基本設(shè)計(jì)思路與實(shí)現(xiàn)方法,簡(jiǎn)單講述了php實(shí)現(xiàn)tp框架的原理,并結(jié)合實(shí)例形式分析了相關(guān)控制器、視圖及URL訪問操作技巧與注意事項(xiàng),需要的朋友可以參考下2018-05-05php中array_fill函數(shù)的實(shí)例用法
在本篇文章里小編給大家整理的是一篇關(guān)于php中array_fill函數(shù)的實(shí)例用法,有興趣的朋友們可以學(xué)習(xí)參考下。2021-03-03PHP實(shí)現(xiàn)簡(jiǎn)單的模板引擎功能示例
這篇文章主要介紹了PHP實(shí)現(xiàn)簡(jiǎn)單的模板引擎功能,結(jié)合實(shí)例形式詳細(xì)分析了PHP實(shí)現(xiàn)模板引擎功能的模版類、編譯類、控制器類及模板文件等實(shí)現(xiàn)方法與相關(guān)操作技巧,需要的朋友可以參考下2017-09-09