@@ -16,21 +16,29 @@ class Response
1616 public function send ($ content , $ http_headers = []) {
1717 if (is_array ($ http_headers ) && !empty ($ http_headers )) {
1818 foreach ($ http_headers as $ key => $ value ) {
19- header ($ key . ': ' . $ value );
19+ if (!headers_sent ()) {
20+ header ($ key . ': ' . $ value );
21+ }
2022 }
2123 }
2224 echo $ content ;
2325 }
2426
2527 public function json ($ data )
2628 {
27- header ('Content-Type: application/json ' );
29+ if (!headers_sent ()) {
30+ header ('Content-Type: application/json ' );
31+ }
2832 echo json_encode ($ data );
2933 }
3034
3135 public function redirect ($ url )
3236 {
33- header ("Location: $ url " );
37+ ob_start ();
38+ if (!headers_sent ()) {
39+ header ("Location: $ url " );
40+ }
41+ ob_end_flush ();
3442 exit ();
3543 }
3644
@@ -112,18 +120,24 @@ public function render($template, $data=[])
112120 }
113121
114122 public function setHeader ($ name , $ value ) {
115- header ($ name .': ' .$ value );
123+ if (!headers_sent ()) {
124+ header ($ name .': ' .$ value );
125+ }
116126 }
117127
118128 public function setSession ($ name , $ value )
119129 {
120- session_start ();
130+ if (session_status () === PHP_SESSION_NONE ) {
131+ session_start ();
132+ }
121133 $ _SESSION [$ name ] = $ value ;
122134 }
123135
124136 public function setCookie ($ name , $ value , $ expires = 0 , $ path = '' , $ domain = '' , $ secure = false , $ httponly = false )
125137 {
138+ ob_start ();
126139 setcookie ($ name , $ value , $ expires , $ path , $ domain , $ secure , $ httponly );
140+ ob_end_flush ();
127141 }
128142
129143 public function status ($ statusCode )
0 commit comments