From 439af54ba6b7a654ba6ad7a3b6b93cdaa2f077d0 Mon Sep 17 00:00:00 2001 From: Urban Müller Date: Mon, 2 May 2022 17:25:18 +0200 Subject: introduce ::json_headers, kill unused var --- itjs.class | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'itjs.class') diff --git a/itjs.class b/itjs.class index 00da2d3..01b971e 100644 --- a/itjs.class +++ b/itjs.class @@ -21,7 +21,6 @@ class itjs { -static $charset; /** * Send HTTP headers (content-type) to transmit javascript code @@ -31,12 +30,24 @@ static function send_headers($charset = null) if (!$charset) $charset = ini_get('default_charset') ?: 'iso-8859-1'; - self::$charset = $charset; - @header("Content-Type: application/x-javascript; charset=$charset"); @header('Expires: ' . gmdate('D, d M Y H:i:s', time()+10) . ' GMT'); # prevent broken data on IE reloads } +/** + * Send HTTP headers (content-type) to send json + * $p['maxage'] Number of seconds the content is valid for without verification [30] + */ +static function json_headers($p = []) +{ + $p += ['charset' => ini_get('default_charset') ?: 'iso-8859-1', 'maxage' => 30]; + + @header("Content-Type: application/json; charset=" . $p['charset']); + if ($p['maxage']) + @header("Cache-Control: max-age=" . $p['maxage']); +} + + /** * json_encode the result with options suitable for using as javascript source code * @param $values Array with values to be serialized -- cgit v1.2.3