From 05c98eb39f07bd951b6047e154db6479828e2a11 Mon Sep 17 00:00:00 2001 From: jvoisin Date: Sun, 12 Jul 2020 19:19:41 +0200 Subject: Use $_SERVER['REMOTE_ADDR'] in last resort to get the client's ip addr --- src/sp_utils.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/sp_utils.c b/src/sp_utils.c index 8c64b55..e5c2942 100644 --- a/src/sp_utils.c +++ b/src/sp_utils.c @@ -19,6 +19,24 @@ const char* get_ipaddr() { return fwd_ip; } + /* Some hosters (like heroku, see + * https://github.com/jvoisin/snuffleupagus/issues/336) are clearing the + * environment variables, so we don't have access to them, hence why we're + * resorting to $_SERVER['REMOTE_ADDR']. + */ + if (!Z_ISUNDEF(PG(http_globals)[TRACK_VARS_SERVER])) { + const zval* const globals_client_ip = + zend_hash_str_find(Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER]), + "REMOTE_ADDR", sizeof("REMOTE_ADDR") - 1); + if (globals_client_ip) { + if (Z_TYPE_P(globals_client_ip) == IS_STRING) { + if (Z_STRLEN_P(globals_client_ip) != 0) { + return estrdup(Z_STRVAL_P(globals_client_ip)); + } + } + } + } + return default_ipaddr; } -- cgit v1.3