2

I'm trying to write a PHP extension that needs to access the currently processed request (the request URL in case of HTTP SAPIs or the file name in case of CLI), basically $_SERVER['REQUEST_URI'] or similar.

Ideally, I'd get the request in RINIT, but RSHUTDOWN works for me as well.

The juicy SAPI methods and variables aren't exported to extensions (which makes sense I suppose), so I tried to access the PHP $_SERVER variable:

zend_hash_str_find(&EG(symbol_table), "_SERVER", strlen("_SERVER"))

It works fine if the PHP script accesses it before my extension does but it's empty otherwise. I expect the contents are lazily generated on first access but I don't know how to trigger the generation.

I want my extension to be as non-invasive as possible, so I'd really like to avoid hacks like auto_prepend_file.

Any pointers/hints?

Grzegorz Nosek
  • 464
  • 3
  • 9
  • So, `$_SERVER` is always empty when you dump it out, or it's not set and throws an error? – Ethan May 10 '18 at 13:46
  • 2
    Internally, the super-global variables like `$_SERVER` are referred to as "auto-globals", and are indeed only referenced when needed rather than being included in every symbol table automatically. I did some digging [in this answer](https://stackoverflow.com/a/47661412/157957) which may lead you in a useful direction. – IMSoP May 10 '18 at 13:54
  • @IMSoP YES! calling `zend_is_auto_global` does the trick, thanks! Feel free to make it an answer so I can accept it :) – Grzegorz Nosek May 10 '18 at 14:17

0 Answers0