The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

what can make $response->withStatus(404) not working on production server but working on local development?

andrea.bisello's Avatar

andrea.bisello

07 Jan, 2016 04:24 PM

Hi

with slim 3 i make a test route to obtain a 404 response :

$app->get('/give404', function ($request, $response, $args) {
    $response->write("test");
    $newResponse = $response->withStatus(404);
    return $newResponse;
});

in local server it works, online it return 200

what change make this issue?

ONLINE :

HTTP/1.1 200 OK
Date: Thu, 07 Jan 2016 16:17:34 GMT
Server: Apache/2
X-Powered-By: PHP/5.5.22
Access-Control-Allow-Origin: *
Vary: Accept-Encoding,User-Agent
Content-Encoding: gzip
Content-Length: 25
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html; charset=utf-8

LOCALHOST

HTTP/1.1 404 Not Found
Date: Thu, 07 Jan 2016 16:17:28 GMT
Server: Apache/2.4.7 (Win32) PHP/5.5.8
X-Powered-By: PHP/5.5.8
Access-Control-Allow-Origin: *
Content-Length: 5
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html; charset=UTF-8

thanks

  1. 1 Posted by andrea.bisello on 07 Jan, 2016 05:23 PM

    andrea.bisello's Avatar

    this is the dump about

    public function withStatus($code, $reasonPhrase = '')

    of Response.php

    LOCALHOST

    test

    selectede code 404

    int(404)

    this is the clone clone

    object(Slim\Http\Response)#48 (5) { ["status":protected]=> int(404) ["reasonPhrase":protected]=> string(0) "" ["protocolVersion":protected]=> string(3) "1.1" ["headers":protected]=> object(Slim\Http\Headers)#43 (1) { ["data":protected]=> array(1) { ["content-type"]=> array(2) { ["value"]=> array(1) { [0]=> string(24) "text/html; charset=UTF-8" } ["originalKey"]=> string(12) "Content-Type" } } } ["body":protected]=> object(Slim\Http\Body)#53 (6) { ["stream":protected]=> resource(70) of type (stream) ["meta":protected]=> array(6) { ["wrapper_type"]=> string(3) "PHP" ["stream_type"]=> string(4) "TEMP" ["mode"]=> string(3) "w+b" ["unread_bytes"]=> int(0) ["seekable"]=> bool(true) ["uri"]=> string(10) "php://temp" } ["readable":protected]=> NULL ["writable":protected]=> bool(true) ["seekable":protected]=> NULL ["size":protected]=> NULL } }

    production environment

    test

    selectede code 404

    int(404)

    this is the clone clone

    object(Slim\Http\Response)#48 (5) { ["status":protected]=> int(404) ["reasonPhrase":protected]=> string(0) "" ["protocolVersion":protected]=> string(3) "1.1" ["headers":protected]=> object(Slim\Http\Headers)#43 (1) { ["data":protected]=> array(1) { ["content-type"]=> array(2) { ["value"]=> array(1) { [0]=> string(24) "text/html; charset=UTF-8" } ["originalKey"]=> string(12) "Content-Type" } } } ["body":protected]=> object(Slim\Http\Body)#53 (6) { ["stream":protected]=> resource(70) of type (stream) ["meta":protected]=> array(6) { ["wrapper_type"]=> string(3) "PHP" ["stream_type"]=> string(4) "TEMP" ["mode"]=> string(3) "w+b" ["unread_bytes"]=> int(0) ["seekable"]=> bool(true) ["uri"]=> string(10) "php://temp" } ["readable":protected]=> NULL ["writable":protected]=> bool(true) ["seekable":protected]=> NULL ["size":protected]=> NULL } }

  2. 2 Posted by andrea.bisello on 07 Jan, 2016 08:05 PM

    andrea.bisello's Avatar

    There is a difference here :

    in App.php

    public function respond(ResponseInterface $response)

    in localhost , headers_sent() is false, on production server is true , but getStatusCode is correct (404)

    public function respond(ResponseInterface $response)
        {
            if(!headers_sent()){
                echo "!headers sent";
            } else {
                echo "headers sent";
            }
            echo "getStatusCode " . $response->getStatusCode();

    !headers sentgetStatusCode 404

    but because in production environment header is alredy there, the

    header(sprintf(
                    'HTTP/%s %s %s',
                    $response->getProtocolVersion(),
                    $response->getStatusCode(),
                    $response->getReasonPhrase()
                ));

    block will not be executed.

  3. 3 Posted by andrea.bisello on 08 Jan, 2016 06:22 PM

    andrea.bisello's Avatar

    i created header.php with this code

    <?php
    header("HTTP/1.0 422 TEST");
    ?>

    and ONLINE i obtain

    Status Code:422 TEST

    so the server sets header.

    but there http://php.net/manual/en/function.headers-sent.php

    i read

    "You can't add any more header lines using the header() function once the header block has already been sent. Using this function you can at least prevent getting HTTP header related error messages. Another option is to use Output Buffering."

    and here http://php.net/manual/en/function.header.php i read that header cannot be changed if everything was already sent.

    can be this the case?

  4. 4 Posted by andrea.bisello on 08 Jan, 2016 08:23 PM

    andrea.bisello's Avatar

    i make a try on three web hosting provider.

    www.joomlahost.it fails : return 200
    cheweb.it fails : it return 200
    http://abioneperhobby.96.lt/api/404 fails, it return 200

    this is the code, based on the new 3.1 slim

    $app->get('/404', function ($request, $response, $args) {
        $response->write("404")->withStatus(404);
    var_dump($response);
        return $response;
    });

  5. 5 Posted by nanaksr on 26 Jan, 2016 08:26 AM

    nanaksr's Avatar

    same problem with me..

  6. 6 Posted by andrea.bisello on 26 Jan, 2016 01:05 PM

    andrea.bisello's Avatar

    i was sure i had answered this issue but not.

    i solved.

    One of my .php classes ended with ?> and this make web server anwsering with some text . text explained why header was already sent.

    after removing ?> ,

    not header was alredy sent,

    and setting the new http status code worked.

    nanaksr try to remove everything and introduce one class at time

  7. 7 Posted by Laxmikant Thanv... on 28 Jul, 2016 10:31 AM

    Laxmikant Thanvi's Avatar

    I don't have any file having ?> on end of any file but still m facing same issue, status is not reflecting as I set in code , m setting 401 its giving 200 in browser Strange ??

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac