5

I have written a PHP extension library in C++. I am writing the extension for PHP 5.x ad above.

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.

2 Answers 2

3

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, ...

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;

Then use SG(request_info).request_urior 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 */

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!

4
  • Hi Johannes, I am looking for the server variables data (typically accessed using $_SERVER in PHP. As documentation on PHP extensions seems to be rarer tahn hen's teeth, I would be extremely grateful if you could post a snippet that would show how to access the array, or one of its elements from the C/C++ side. You may either post the snippet here, or at patebin.com. MTIA Dec 15, 2009 at 12:01
  • Johannes: You Sir, are a scholar and a gentleman!. This is all I need to get me started. I am forever grateful. I will accept this as my answer. Dec 15, 2009 at 12:29
  • Sorry to unburrow this question, but shouldn't we reactivate jit after we're done with our var? ("YOUR_VARNAME", in this case)
    – ravemir
    Aug 28, 2012 at 10:41
  • 1
    @ravemir no that's not how this "diabling" works. "disabling" causes the array to be populated. This happens automatically if the parser detects it in a PHP file. Once it's populated it is there. The whole purpose is to avoid the time populating it in cases where it isn't needed at all.
    – johannes
    Aug 28, 2012 at 14:05
0

i guess you need something like zend_hash_find(CG(auto_globals),... but i'm no expert

1
  • 1
    CG = compiler_globals, CG should only be used while compiling a script.
    – johannes
    Dec 15, 2009 at 11:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.