我有一个简单的链接:<a href="?wp_accept_function=10">Accept</a>
这个想法是,这将运行一个名为wp_accept_function的函数,并传入10的id,我该怎么做?感谢
这是我到目前为止的代码,但我觉得我出错了,需要将数字传递给函数,然后才能在函数中使用它。感谢
if ( isset ( $_GET ['wp_accept_function'] ) )
{
function wp_accept_favor ( $id )
{
// JAZZ
}
}
答案 0 :(得分:7)
我想你想要这个:
首先,您需要定义该功能。
function wp_accept_favor($id) {
// do something
}
然后,您必须检查参数是否已设置并调用该函数。
if (isset($_GET['wp_accept_function'])) {
// call the function passing the id casted to an integer
wp_accept_favor((int)$_GET['wp_accept_function']);
}
转换为(int)
是为了避免为wp_accept_favor()
函数传递非整数类型,但您可以根据需要处理它。
答案 1 :(得分:2)
如果您正在尝试构建通用的东西......
// Add a white list of functions here which can be called via GET.
$safeFunctions = array('wp_accept_function');
foreach($_GET as $function => $argument) {
if (in_array($function, $safeFunctions)
AND function_exists($function)) {
$function($argument);
}
}
但是,请确保您拥有安全功能的白名单,否则您的应用无疑会遇到安全问题。