如何截获脚本中的PHP函数调用? [英] How to intercept PHP function call in my script?
问题描述
我知道我可以使用 __ call
魔术方法拦截对象的方法调用.但是我该如何在脚本部分截获PHP函数(内置于PHP或由用户定义)呢?
I know that I can intercept method call with __call
magic method for objects. But how I can intercept a PHP function (build in PHP or defined by user) call in my part of script?
例如,我有一些代码:
echo "Hello world";
$s = 0;
$i = $i + 5;
$str = str_repeat('Hi ', $i);
$hash = md5($str);
print $a;
我需要拦截 str_repeat()
函数调用,并且不执行它及其后的所有代码并返回一些错误(或简单地执行 die('error');
),这是真的吗?
I need to intercept str_repeat()
function call and do not execute it and all code after it and return some error (or simply execute die('error');
), is it real?
推荐答案
did you check this : http://www.java2s.com/Code/Php/Class/InterceptingMethodCallswiththecallMethodPHP5Only.htm ?
这并不复杂,但是您需要编写一些代码.
It's not complicated but you need to write a bit of code.
您还可以创建自己的拦截函数,该回调函数可以进行回调,而无需调用函数/方法,您可以通过自己的拦截函数传递它.
You can also create your own intercepting function that takes a callback and instead of calling your function/method you can pass it through your own intercepting function.
伪代码
function myFakeFunction(...){ ... }
function runFunction($functionName){
// DO WHAT YOU WANT: INTERCEPTION
call_user_func($functionName);
}
runFunction("myFakeFunction", ...);
在文档中
注意:您应该关心两种方法的安全性问题.
NOTE: you should care about security issues for both methodologies.
我只是一个新主意,您还可以为要检查的所有方法创建一个共享对象,并在该对象上使用_call.但是我不确定那是否可以正常工作.
I just have a new idea, you can also create a shared object for all the methods that you want to check and use _call on that object. But I'm not sure if that would work correctly.
这篇关于如何截获脚本中的PHP函数调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!