Skip to content

Commit 0310f38

Browse files
committed
Added missing file for "Bug in Firefox: Remote XMLHttpRequest" example
1 parent 640426d commit 0310f38

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

shared/json_echo.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?PHP?><?
2+
3+
//
4+
// Simple PHP JSON/P Echo - "Cowboy" Ben Alman - http://benalman.com/
5+
// v1.0 - 2/20/2009
6+
//
7+
8+
// Show script source if no params are passed
9+
if (count($_GET) == 0) {
10+
highlight_file($_SERVER['SCRIPT_FILENAME']);
11+
die;
12+
}
13+
14+
// Get request params
15+
$obj = array();
16+
foreach ($_GET as $key => $value) {
17+
$obj[$key] = $value;
18+
}
19+
foreach ($_POST as $key => $value) {
20+
$obj[$key] = $value;
21+
}
22+
23+
$json_string = isset($obj['JSON']) ? $obj['JSON'] : null;
24+
$jsonp_callback = isset($obj['callback']) ? $obj['callback'] : null;
25+
26+
// remove misc unneeded params
27+
unset($obj['_']);
28+
unset($obj['callback']);
29+
unset($obj['JSON']);
30+
31+
$json = $json_string ? $json_string : json_encode($obj);
32+
$jsonp = $jsonp_callback ? $jsonp_callback . "($json)" : $json;
33+
34+
35+
sleep(1); // simulate slow connection :D
36+
37+
38+
$is_xhr = isset($_SERVER['HTTP_X_REQUESTED_WITH']) &&
39+
strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
40+
41+
$is_referer = isset($_SERVER["HTTP_REFERER"]);
42+
43+
if ($is_xhr) {
44+
45+
header( 'Content-type: application/json' );
46+
print $jsonp;
47+
48+
} else if ($is_referer) {
49+
50+
$params = array();
51+
foreach ($obj as $key => $value) {
52+
$params[] = urlencode($key) . '=' . urlencode($value);
53+
}
54+
55+
$url = preg_replace('/\?.*$/', '', $_SERVER["HTTP_REFERER"]);
56+
$url .= '?' . implode('&', $params);
57+
58+
header( "Location: $url");
59+
60+
} else {
61+
62+
header( 'Content-type: text/plain' );
63+
64+
print $jsonp;
65+
66+
}
67+
68+
?>

0 commit comments

Comments
 (0)