PHP superglobal'leri erişen PHP uzantısı kitaplığı

2 Cevap php

Ben C + + PHP uzantısı kütüphanesi yazdım. Ben yukarıdaki PHP 5.x reklamın uzantısı yazıyorum.

Benim C + + kod PHP superglobal'leri erişmek gerekir. Herkes bunun nasıl biliyor mu?. Benzer bir kaynak (hayır cinas ...) için bir kod parçası veya işaretçi (inteded hayır cinas) büyük mutluluk duyacağız.

2 Cevap

Aslında ne veri gerekiyor? - Çoğu veri için en iyi yolu geliyorlar C yapısını ifade etmektir. Istek verileri ile Örneğin siz kontrol edebilirsiniz sapi_globals, erişilebilir kullanarak SG() makro, oturum verileri oturum modülü üzerinden ulaşılabilir, ...

Eğer gerçekten EG(symbol_table) karma tabloda bulabilirsiniz bir süper küresel erişim gerekiyorsa. PHP yalnızca gerektiğinde bu devre dışı bırakmak için ilk zend_auto_global_disable_jit() aramak gerekebilir süper küresellerle sağlamak için bir JIT mekanizması vardır gibi.


Aşağıdaki Yorum Cevaplama: Bu yeterince herhangi bir veri var mı:

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;

Sonra sadece bu değerleri okumak gerekir iken yazmak değil, SG(request_info).request_uri veya benzeri kullanmak, yani gerekirse bir kopyasını yapmak.

Bunların hiçbiri yeterli mi? - Sonra geri Ben yukarıda söyledikleri gidin:

/* 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!

i zend_hash_find(CG(auto_globals),... gibi bir şey gerekir ama ben uzman değilim sanırım