-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprg.php
121 lines (102 loc) · 3.51 KB
/
prg.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
namespace diversen;
/**
*
*/
class prg {
/**
* Generate redirect for use in prg
* @return string $path path and query
*/
private static function getRedirect(){
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$ary = array();
foreach($_GET as $key => $val) {
if ($key == 'q') {
continue;
}
if ($key =='prg' OR $key == 'uniqid') {
continue;
}
$ary[$key] = $val;
}
$query = http_build_query($ary);
$ret = $path . '?' . $query;
if (!empty($ary)) {
$ret.='&';
}
return $ret;
}
/**
* Simple pattern for creating PRG.
* (Keep state when reloading browser and resends forms etc.)
* @param int $last
*/
public static function prg ($max_time = 0){
// genrate a session var holding the _POST
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
$uniqid = uniqid();
$_SESSION['post'][$uniqid] = $_POST;
$_SESSION['post'][$uniqid]['prg_time'] = time();
$_SESSION['REQUEST_URI'] = $_SERVER['REQUEST_URI'];
header("HTTP/1.1 303 See Other");
$location = self::getRedirect() . 'prg=1&uniqid=' . $uniqid;
self::locationHeader($location);
}
if (!isset($_SESSION['REQUEST_URI'])){
$_SESSION['post'] = null;
} else {
if (isset($_GET['prg'])){
$uniqid = $_GET['uniqid'];
if (isset($_SESSION['post'][$uniqid])) {
if ( $max_time && ($_SESSION['post'][$uniqid]['prg_time'] + $max_time) < time() ) {
unset($_SESSION['post'][$uniqid]);
} else {
$_POST = $_SESSION['post'][$uniqid];
}
}
} else {
@$_SESSION['REQUEST_URI'] = null;
}
}
}
/**
* Simple function for creating prg pattern.
* (Keep state when reloading browser and resends forms etc.)
*/
public static function prgSinglePost (){
// POST
// genrate a session var holding the _POST
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
$uniqid = uniqid();
$_SESSION['post'][$uniqid] = $_POST;
$_SESSION['post'][$uniqid]['prg_time'] = time();
$_SESSION['REQUEST_URI'] = $_SERVER['REQUEST_URI'];
header("HTTP/1.1 303 See Other");
$location = self::getRedirect() . 'prg=1&uniqid=' . $uniqid;
self::locationHeader($location);
}
if (!isset($_SESSION['REQUEST_URI'])){
$_SESSION['post'] = null;
} else {
if (isset($_GET['prg'])){
$uniqid = $_GET['uniqid'];
if (isset($_SESSION['post'][$uniqid])) {
$_POST = $_SESSION['post'][$uniqid];
unset($_SESSION['post'][$uniqid]);
}
} else {
@$_SESSION['REQUEST_URI'] = null;
}
}
}
/**
* Send a location header
* @param type $location the location, e.g. /content/view/article/3
*/
public static function locationHeader ($location) {
$header = "Location: $location";
header($header);
die();
}
}