/** * Get (or init) session counter(s). * * Counters will only be set and updated if conf var session_counters, * because this feature uses a cookie, and it also adds a slight performance * hit to each and every request. * * Counters: * - session: not a number, a custom session id (case-sensitive hex, approx. * 16 chars long), or simply 'na' (as in n/a) * - page_load: number of request in session that produced 'page' (non-AJAX) * output * - request: number of requests in session * * @param string $name * Default: empty (~ get all, as array). * Values: session|page_load|request (~ get single value). * * @return mixed * Array: all counters. * String: session. * Integer: page_load|request. * NULL: bad arg $name. */ public static function sessionCounters($name = '') { static $called; // Init first time called. if (!$called) { $called = TRUE; if (static::configGet('inspect', 'session_counters')) { if (($c = static::cookieGet('inspect__sc')) && preg_match('/^[a-zA-Z\\d]+\\:\\d{1,5}\\:\\d{1,5}$/', $c)) { $c = explode(':', $c); static::$sessionCounters = $counters = array('session' => $c[0], 'page_load' => $c[1], 'request' => (int) $c[2] + 1); } else { $c = explode('.', uniqid('', TRUE)); static::$sessionCounters = $counters = array('session' => Inspect::baseConvert($c[0], 16, 62) . Inspect::baseConvert($c[1], 16, 62), 'page_load' => 0, 'request' => 1); } if (!headers_sent()) { // Session counting is not important enough to risk PHP warning due // to response body sending already commenced. static::cookieSet('inspect__sc', join(':', static::$sessionCounters)); } } else { $counters = static::$sessionCounters; } } else { $counters = static::$sessionCounters; } if ($name) { switch ('' . $name) { case 'session': case 'page_load': case 'request': return $counters[$name]; } return NULL; } return $counters; }