r/PHPhelp • u/nickk21321 • Sep 12 '25
My PHP http_response_code is not sending status 200 but status code 302?
Hi all I am facing an issue with my Webhook response code whereby I am sending a Http response code of 302 (redirect) based on my http server logs.
This 302 redirect is overriding my default HTTP response code 200 I have set up in my scripts.
The 302 redirect is coming from the require_once in my main script below but it is after my http_response_code(200).
Any suggestion how do I ensure only my main script's http_responce_code(200) is being sent out and not the require_once files.
We are using PHP version 5 on our end.
Code snippet as below:
if (hash_equals($provided_signature, $yourHash)) {
http_response_code(200);
if ($product === 'v1'){
$pymntGateway='curlec';
require_once "v1_backend.php"; (302 redirect from here)
}
elseif ($product === 'v2'){
$pymntGateway='curlec';
require_once "v2_backend.php"; (302 redirect from here)
}
else{
http_response_code(202);
exit('Unknown product.');
}
}
else{
http_response_code(403); // Forbidden
exit('Invalid signature.');
}
2
Upvotes
3
u/Important_Material92 Sep 12 '25
Are you saying that your v1_backend.php and v2_backend.php files contains a 302 header? If so you could use a if(!headers_sent()) in those files to only send a header if they haven’t been sent already?
1
u/allen_jb Sep 12 '25
require_oncewould not cause a HTTP Status change. Look at the code being executed by the required scripts.The likely sources are:
Check your code for use of the
header()orhttp_response_code()functions (or similar functions from your framework).Check your webserver configuration for redirects / URL rewrites.
The webserver (and where available php-fpm) request logs may help you determine exactly what's happening at what point.
2
u/armahillo Sep 12 '25
Granted, I haven't written PHP in a long time but I also wrote it for as long:
Don't do that.
Require the files at the top of the script, and then have those files have classes / functions that are then called. ie.
1
u/Hackwar Sep 12 '25
You posted the same in r/php except that here you neglect to say that you are using PHP 5. https://www.reddit.com/r/PHP/comments/1nexuc6/my_php_http_response_code_is_not_sending_status/
1
u/ardicli2000 Sep 12 '25
Redirect 302 will overwrite 200