我开发了一个jQuery插件,允许你调用任何核心PHP函数,甚至用户定义的PHP函数作为插件的方法:jquery.php
包括jQuery和jquery.php在我们的文档的头部和在放置后request_handler.php在我们的服务器上,我们将以下面描述的方式开始使用插件。
为了便于使用,引用功能以简单的方式:
var P = $.fn.php;
然后初始化插件:
P('init',
{
// The path to our function request handler is absolutely required
'path': 'http://www.YourDomain.com/jqueryphp/request_handler.php',
// Synchronous requests are required for method chaining functionality
'async': false,
// List any user defined functions in the manner prescribed here
// There must be user defined functions with these same names in your PHP
'userFunctions': {
languageFunctions: 'someFunc1 someFunc2'
}
});
现在一些使用场景:
// Suspend callback mode so we don't work with the DOM
P.callback(false);
// Both .end() and .data return data to variables
var strLenA = P.strlen('some string').end();
var strLenB = P.strlen('another string').end();
var totalStrLen = strLenA + strLenB;
console.log(totalStrLen); // 25
// .data Returns data in an array
var data1 = P.crypt("Some Crypt String").data();
console.log(data1); // ["$1$Tk1b01rk$shTKSqDslatUSRV3WdlnI/"]
演示PHP函数链:
var data1 = P.strtoupper("u,p,p,e,r,c,a,s,e").strstr([], "C,A,S,E").explode(",", [], 2).data();
var data2 = P.strtoupper("u,p,p,e,r,c,a,s,e").strstr([], "C,A,S,E").explode(",", [], 2).end();
console.log(data1, data2);
证明发送的PHP伪码的JSON块:
var data1 =
P.block({
$str: "Let's use PHP's file_get_contents()!",
$opts:
[
{
http: {
method: "GET",
header: "Accept-language: en\r\n" +
"Cookie: foo=bar\r\n"
}
}
],
$context:
{
stream_context_create: ['$opts']
},
$contents:
{
file_get_contents: ['http://www.github.com/', false, '$context']
},
$html:
{
htmlentities: ['$contents']
}
}).data();
console.log(data1);
后端配置提供白名单,以便限制其功能可以被调用。还有一些与插件描述的PHP一起工作的其他模式。
通过PHP javascript生成的PHP代码被评估,或者以相反的方式进行,这是一个非常糟糕的主意。 – 2010-02-15 22:18:10
你能解释为什么这样不好吗? – Catfish 2010-02-15 22:22:57
因为任何人都可以将任何代码放在您的代码的位置,因此他们可以对您的服务器做坏事。 – 2010-02-15 22:27:20