我用 C++ 编写了一个 PHP 扩展库。我正在为上面的 PHP 5.x 广告编写扩展。
我需要在我的 C++ 代码中访问 PHP 超全局变量。有谁知道如何做到这一点?。指向类似资源(无双关语......)的代码片段或指针(无双关语)将不胜感激。
您真正需要什么数据?- 对于大多数数据来说,最好的方法是引用它们来自的 C 结构。例如,使用请求数据,您可以检查sapi_globals,使用SG()宏访问,会话数据可通过会话模块获得,...
如果您确实需要访问超级全局,您可以在EG(symbol_table)哈希表中找到它。由于 PHP 有一个 JIT 机制,仅在需要时才提供超级全局变量,因此您可能需要zend_auto_global_disable_jit()首先调用来禁用它。
回答下面的评论:这些数据是否足够:
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;
Run Code Online (Sandbox Code Playgroud)
然后使用SG(request_info).request_uri或类似的方法,而您应该只读取这些值,而不是写入,因此如果需要,请复制一份。
这些还不够吗?——然后回到我上面说的:
/* 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 */
Run Code Online (Sandbox Code Playgroud)
请注意,我只是在没有检查任何内容的情况下从我的索引中输入它,因此它可能是错误的,包含拼写错误等。并且请注意:您应该意识到这样一个事实,即您必须使用sizeof()notsizeof()-1与哈希 API 作为终止 null -byte 是计算的哈希的一部分,并且函数返回 SUCCESS 或 FAILURE,而SUCCESS被定义为0和FAILUREas -1,这不是人们所期望的,所以总是使用这些常量!
| 归档时间: |
|
| 查看次数: |
1233 次 |
| 最近记录: |