doufu6504 2011-11-10 15:32
浏览 25
已采纳

header()如何工作?

A little context:
I needed to run PHP scripts in a browser, but I didn't want to go to all the trouble of installing a server and suffer the overhead of running a server on my computer and all the stuff that goes with it, including firewalls, blah blah blah.

So instead I wrote my own server. It's a simple PHP script that listens for connections on port 80 of my LAN IP, then I just load that IP in my browser and it works. It receives the HTTP request and starts a second PHP script using exec - this is so that I can make changes to it easily without having to restart the server script. This second PHP script parses the request, and finally includes the script that was actually being called. It gets the output from there, and sends the response back to the browser with appropriate headers (which I can change).

Yeah, it's a mess, but it works. It does what I need it to do.

Now for the question:
I can't use header(). It doesn't seem to be having any effect on what gets sent back to the browser through the socket connection. I have instead made a setheader() function, and store headers in an array to be prepended to the response.

I'd like to know how the header() function actually works internally, so that I might be able to use that function instead of my "hacked" one.

  • 写回答

7条回答 默认 最新

  • drrrdo0802 2011-11-10 16:47
    关注

    The header() function is totally ignored by the CLI SAPI. It has an effect on the Apache and CGI SAPIs though.

    Simply put, the CLI SAPI doesn't implement any logic in the sapi_*_header_* functions. Per example, for the CLI SAPI, in php_cli.c:

    static int sapi_cli_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC) /* {{{ */
    {
        /* We do nothing here, this function is needed to prevent that the fallback
        * header handling is called. */
        return SAPI_HEADER_SENT_SUCCESSFULLY;
    }
    /* }}} */
    

    All those functions basically return NULL, 0 or a fake success message.

    For the CGI SAPI, in cgi_main.c:

    static int sapi_cgi_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC)
    {
        char buf[SAPI_CGI_MAX_HEADER_LENGTH];
        sapi_header_struct *h;
        zend_llist_position pos;
        zend_bool ignore_status = 0;
        int response_status = SG(sapi_headers).http_response_code;
    
        // Lots of other code...
    }
    

    You can easily make this work using the php-cgi binary and some array manipulation:

    server.php

    $script_to_run = 'script.php';
    exec('php-cgi '.escapeshellarg($script_to_run), $output);
    $separator = array_search("", $output);
    
    $headers = array_slice($output, 0, $separator);
    $body = implode("
    ", array_slice($output, $separator+1));
    
    print_r($headers);
    print $body;
    

    script.php

    header('Content-Type: text/plain');
    echo 'Hello, World!';
    

    Output:

    Array
    (
        [0] => X-Powered-By: PHP/5.3.8
        [1] => Content-Type: text/plain
    )
    Hello, World!
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100