Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6788235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:27:42+00:00 2026-05-26T17:27:42+00:00

A little context: I needed to run PHP scripts in a browser, but I

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:27:42+00:00Added an answer on May 26, 2026 at 5:27 pm

    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("\r\n", 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!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A little context first: I would say I have good SQL server experience, but
Skip to bottom for question, but first, a little context. So I have been
This is going to be a little long and rambly, but I want to
A little help needed. I'm receiving an xml file similar to this: <?xml version=1.0
I'm currently working on saving larger object structures which contain all the data needed
When I began using jQuery a little over a year ago, I needed to
I have some TT templates that I want tidy-up a little. I use tidy
Little context: We are a team of 6 developers working a web application. Since
I have a little problem with my PHP while loop. I am looping out
So to give a little context, my goal here is to produce a binary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.