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.
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 theSG()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 callzend_auto_global_disable_jit()first to disable this.Answering the comment below: Is any of this data enough:
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:
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()notsizeof()-1with hash APIs as the terminating null-byte is part of the calculated hash and has functions return SUCCESS or FAILURE, whileSUCCESSis defined as0andFAILUREas-1which is not what one might expect, so always use these constants!