PHP扩展库访问PHP超级元组 [英] PHP extension library accessing PHP superglobals
问题描述
我在C ++中编写了一个PHP扩展库。我在上面写了PHP 5.x广告的扩展。
I have written a PHP extension library in C++. I am writing the extension for PHP 5.x ad above.
我需要在我的C ++代码中访问PHP superglobals。有谁知道如何做到这一点?
I need to access PHP superglobals in my C++ code. Does anyone know how to do this?. A code snippet or pointer (no pun inteded) to a similar resource (no pun ...) would be greatly appreciated.
推荐答案
你真正需要什么数据? - 大多数数据的最佳方式是引用它们来自的C结构。例如对于请求数据,您可以检查 sapi_globals
,可以使用 SG()
宏访问,会话数据可通过会话模块...
What data do you actually need? - Best way for most data is to refer to the C structure they are coming from. For instance with request data you can check the sapi_globals
, accessible using the SG()
macro, session data is available via the session module, ...
如果你真的需要访问一个超级全局,你可以在 EG(symbol_table)
散列表。因为PHP有一个JIT机制,只在需要时提供超级全局变量,你可能需要先调用 zend_auto_global_disable_jit()
才能禁用它。
If you really need access to a super global you can find it in the EG(symbol_table)
hash table. As PHP has a JIT mechanism to provide super globals only when needed you might need to call zend_auto_global_disable_jit()
first to disable this.
回答下列意见:这些资料是否足够:
Answering the comment below: Is any of this data enough:
typedef struct {
const char *request_method;
char *query_string;
char *post_data, *raw_post_data;
char *cookie_data;
long content_length;
uint post_data_length, raw_post_data_length;
char *path_translated;
char *request_uri;
const char *content_type;
zend_bool headers_only;
zend_bool no_headers;
zend_bool headers_read;
sapi_post_entry *post_entry;
char *content_type_dup;
/* for HTTP authentication */
char *auth_user;
char *auth_password;
char *auth_digest;
/* this is necessary for the CGI SAPI module */
char *argv0;
/* this is necessary for Safe Mode */
char *current_user;
int current_user_length;
/* this is necessary for CLI module */
int argc;
char **argv;
int proto_num;
} sapi_request_info;
typedef struct _sapi_globals_struct {
void *server_context;
sapi_request_info request_info;
sapi_headers_struct sapi_headers;
int read_post_bytes;
unsigned char headers_sent;
struct stat global_stat;
char *default_mimetype;
char *default_charset;
HashTable *rfc1867_uploaded_files;
long post_max_size;
int options;
zend_bool sapi_started;
time_t global_request_time;
HashTable known_post_content_types;
} sapi_globals_struct;
然后使用 SG(request_info).request_uri
或类似的,而你只应该阅读这些值,而不是写,所以如果需要,请复制。
Then use SG(request_info).request_uri
or similar, while you should only read these values, not write, so make a copy if needed.
这些都不够吗? - 然后回到我上面说的:
None of these is enough? - Then go back to what I said above:
/* untested code, might need some error checking and stuff */
zval **server_pp;
zval **value_pp;
zend_auto_global_disable_jit("_SERVER", sizeof("_SERVER")-1 TSRMLS_CC);
if (zend_hash_find(EG(symbol_table), "_SERVER", sizeof("_SERVER"), (void**)&server_pp) == FAILURE) {
zend_bailout(); /* worst way to handle errors */
}
if (Z_TYPE_PP(server_pp) != IS_ARRAY) {
zend_bailout();
}
if (zend_hash_find(Z_ARRVAL_PP(server_pp), "YOUR_VARNAME", sizeof("YOUR_VARNAME"), (void**)&value_pp) == FAILURE) {
zend_bailout();
}
/* now do something with value_pp */
请注意我jsut在这里输入我的ind,不检查任何东西,所以它可能是错误的,包含错字等。
和作为注释:你应该知道,你必须使用 sizeof ()不是
这不是一个可能的期望,所以总是使用这些常数! sizeof() - 1
与散列API作为终止空字节是计算散列的一部分,并且具有函数返回SUCCESS或FAILURE ,而 SUCCESS
定义为 0
和 FAILURE
code> -1
Please mind that I jsut typed it here out of my ind without checking anything so it can be wrong, contain typos etc.
And as a note: You should be aware of the fact that you have to use sizeof()
not sizeof()-1
with hash APIs as the terminating null-byte is part of the calculated hash and has functions return SUCCESS or FAILURE, while SUCCESS
is defined as 0
and FAILURE
as -1
which is not what one might expect, so always use these constants!
这篇关于PHP扩展库访问PHP超级元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!