Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Header in PHP overrides the HTTP Response code

Working around setting rules at Apache level, Found that header('Location: filename.php') in php overrides the response code. Look at the below code :-

<?php
header('HTTP/1.1 308 Permanent Redirect'); //or Try for 4xx/5xx Code
header('Location: http://hello.php');
?>

Expected result

  • HTTP/1.1 308 Permanent Redirect
  • Host: localhost:8000
  • Location: hello.php
  • Connection: close
  • X-Powered-By: PHP/5.5.6

Actual result

  • HTTP/1.1 302 Found
  • Host: localhost:8000
  • Location: hello.php
  • Connection: close
  • X-Powered-By: PHP/5.5.6

Is something wrong at Apache level or its Bug in PHP ?

Update:ScreenShot

like image 840
Vivek Pratap Singh Avatar asked Apr 20 '26 14:04

Vivek Pratap Singh


1 Answers

As per the function documentation page this is the desired behaviour of PHP when sending the Location header.

EDIT: Sorry, misread your message. It will override the response code if you send a code different than 3xx, so it should work with a 308. Are you sure that no other HTTP code is sent back ? Does your client tries to retrieve the new resource and you see the 302 as a final result ?

FINAL EDIT: Did you try

header('Location: /some/page.php', TRUE, 308);
like image 68
NaeiKinDus Avatar answered Apr 22 '26 03:04

NaeiKinDus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!