-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_util.php
72 lines (58 loc) · 1.83 KB
/
http_util.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
// A series of functions useful for interaction with HTTP requests
function safeGetHTTPParam_string( $param_name )
{
if( !isset( $_REQUEST[$param_name] ) )
{
// error_log( "$param_name not set in " . __FILE__ );
return NULL;
}
$value = strval( $_REQUEST[$param_name] );
if( !is_string( $value ) || ( 0 == strlen( $value ) ) )
{
error_log( "invalid $param_name=$value (expecting a string received a " . gettype( $value ) . ") in " . __FILE__ );
return NULL;
}
return $value;
}
function safeGetHTTPParam_pos_int( $param_name )
{
if( !isset( $_REQUEST[$param_name] ) )
{
// error_log( "$param_name not set in " . __FILE__ );
return NULL;
}
$value = $_REQUEST[$param_name];
if( !is_numeric( $value ) )
{
error_log( "invalid $param_name=$value (expecting a pos int received a " . gettype( $value ) . ") in " . __FILE__ );
return NULL;
}
$value = intval( $_REQUEST[$param_name] );
if( !is_numeric( $value ) || ( !is_int( $value ) ) )
{
error_log( "invalid $param_name=$value (expecting a pos int received a " . gettype( $value ) . ") in " . __FILE__ );
return NULL;
}
return $value;
}
function sendResult( $result, $allow_caching = false )
{
if( !$allow_caching )
{
// Results are dynamic, tell the recipient not to cache the results
if( headers_sent( $file, $line ) )
{
error_log( "HTTP headers already sent from $file( $line )" );
}
else
{
header( 'Expires: 0' );
header( 'Cache-Control: must-revalidate, proxy-revalidate, no-cache, no-store, max-age=0, s-maxage=0' );
}
}
// $json = json_encode( $result, JSON_HEX_TAG|JSON_HEX_APOS|JSON_HEX_QUOT|JSON_HEX_AMP );
$json = json_encode( $result );
echo $json;
}
?>