PHP JSON output headers

A default set of headers that can be used when outputting JSON:
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 
header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT"); 
header("Cache-Control: no-cache, must-revalidate"); 
header("Pragma: no-cache");
header("Content-type: application/json");

echo json_encode($output);


While normal ANSI and/or text/html output works with most JavaScript applications that simply expects pure text back, it's important to use a proper JSON header to prevent XSS (cross site scripting) abuse. Here's a nice blog article I found about it.

This document was last updated July 9th, 2011.
Written by: Dag Jonny Nedrelid
©2007-2012 http://thronic.com


Feel free to leave a comment.
Name:
URL:
0