访问 PHP superglobals 的 PHP 扩展库

标签 php c++

我用 C++ 编写了一个 PHP 扩展库。我正在为上面的 PHP 5.x 广告编写扩展。

我需要在我的 C++ 代码中访问 PHP superglobals。有谁知道如何做到这一点?。将不胜感激指向类似资源(无双关语...)的代码片段或指针(无双关语)。

最佳答案

您实际需要什么数据? - 大多数数据的最佳方式是引用它们来自的 C 结构。例如,对于请求数据,您可以检查 sapi_globals,可使用 SG() 宏访问, session 数据可通过 session 模块获得,...

如果您确实需要访问 super 全局变量,您可以在 EG(symbol_table) 哈希表中找到它。由于 PHP 具有仅在需要时提供 super 全局变量的 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;

然后使用 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 */

请注意,我只是在没有检查任何东西的情况下从我的 ind 中输入它,所以它可能是错误的,包含拼写错误等。 请注意:您应该意识到这样一个事实,即您必须将 sizeof() 而不是 sizeof()-1 与哈希 API 一起使用,因为终止空字节是计算散列的一部分,函数返回 SUCCESS 或 FAILURE,而 SUCCESS 定义为 0FAILURE 定义为 -1 这不是人们所期望的,因此请始终使用这些常量!

关于访问 PHP superglobals 的 PHP 扩展库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1906565/

相关文章:

php - 奇怪的 MYSQL/PHP 错误 : inserts with now()+$i as timestamp occasionally inserts 0

php - Symfony 2.6 登录随机失败

php - 简单的 PHP 查询在一个页面上有效,在另一个页面上崩溃

php - 如何在网络服务器压力测试期间监控内存消耗?

c++ - vscode g++ 链接失败 : Undefined symbols for architecture x86_64

c++ - 是否可以在不完全重构的情况下在现有 C++ 程序中使用(某些)AOP 概念?

c++ - 使用字符串文字和不同字符列表初始化数组声明时有什么区别?

c++ - 我应该丢弃 boost::python::exec 的返回值吗?

php - 我如何获取、查看和更新​​存储在数据库中的复选框值

c++ - '注意:候选:Volume& Volume::operator=(const Volume&)'。这是什么错误?