php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #77841 http_response_code returns the HTTP response code from the CLI
Submitted: 2019-04-03 21:28 UTC Modified: -
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: krowe dot dev at gmail dot com Assigned:
Status: Open Package: HTTP related
PHP Version: Irrelevant OS: ALL
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: krowe dot dev at gmail dot com
New email:
PHP Version: OS:

 

 [2019-04-03 21:28 UTC] krowe dot dev at gmail dot com
Description:
------------
The manual page for the http_response_code function states:

"FALSE will be returned if response_code is not provided and it is not invoked in a web server environment (such as from a CLI application)."

Which is the way this behaves when http_response_code() has not been called already. However, if you call http_response_code() and pass it a value, further calls to http_response_code() without a value will return the value you've set even from a CLI environment.

This side effect could be a issue with any code that is written to work when called from any environment using this function to determine environment type. This issue could instantly be fixed by rewording the manual to more accurately describe the behavior of this function. IMHO PHP really *SHOULD* provide a definitive way to determine if we should be rendering HTML or plain text and, if this worked as advertised, it would fit that use case perfectly well. As it is, it does work but only if you can guarantee that nothing else is mucking about with the response code. This makes it unreliable to use in situations where the response code may have been set.

This problem is easily worked around by making sure that you store the value of http_response_code() before you call it with any parameters. Still, this is not the expected behavior given the wording of the manual and it also makes this function less useful as a simple check to determine if the script is being ran from a CLI environment.

I've verified that this is the case on PHP version 5.6.29 and is still a bug in version 7.2.2 . I have not checked any other versions.


Test script:
---------------
<?php

// $is_cli is properly set to FALSE even though the response code defaults
// to 200 in all environments. However, this will no longer work after the
// next call to http_response_code().
$is_cli=http_response_code(); 
var_dump($is_cli);

// Calling http_response_code() with a status code causes the bug to occur.
http_response_code(300);

// This is the bug. $is_cli should be FALSE if this is being ran from the CLI.
// In a web environment, it should now be 300.
$is_cli=http_response_code();
var_dump($is_cli);



Expected result:
----------------
Expected result in a CLI environment:

bool(false)
bool(false)


Expected result in a web environment:

int(200)
int(300)



Actual result:
--------------
Actual result in a CLI environment:

bool(false)
int(300)


Actual result in a web environment:

int(200)
int(300)


Patches

Pull Requests

Pull requests:

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-05-08 17:14 UTC]
The following pull request has been associated:

Patch Name: Fixes for #77841
On GitHub:  https://github.com/php/php-src/pull/4131
Patch:      https://github.com/php/php-src/pull/4131.patch
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 06:01:30 2024 UTC