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 3322976
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:14:49+00:00 2026-05-17T23:14:49+00:00

sorry for that title but it is very hard to explain in a few

  • 0

sorry for that title but it is very hard to explain in a few words.

I wrote a little web proxy -not apache or any kind of common webserver- who’s role is to execute some php code.

There are two ways of do it:

1) fork a new php -f file.php

2) call http://localhost/file.php from within the web proxy.

I think there would be a lot of concurrent requests to that proxy, and each request will keep alive for at least 20-30 seconds.

My question is: which is better between forking and requesting via http ?

Thanks for any hint!

Dario

  • 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-17T23:14:50+00:00Added an answer on May 17, 2026 at 11:14 pm

    I did a proxy too, recently. And – there’s a third option. Doesn’t even need to call itself or another script, it’s completely self-contained, and cross-platform…

    So – first thing is that I have done this using sockets. I guess you did too, but just writing it here in case you did not. I set the proxy in browser to a specific port, which is allowed through firewall into the listening PHP script. To make it start listening, I have to “run” the script on port 80, so I also get a nice real-time console.

    So – the script listens on the socket, and it is set to NON-BLOCKING. It works with a loop (infinite, times out using break when needed) – *@socket_accept()* is used to see if there is any new connection, checked if !== false.

    Finally, the loop loops (:D) through all of the spawned children sockets, which are stored in an array. It’s an HTTP proxy, so all of them have a few specific stages (client sending headers, trying to reach remote server, sending client headers, receiving data from remote server). Things like reading which would (on remote socket opened with fsockopen()) normally be blocked, and there is no way to set that to non-blocking on these, are “emulating” non-blocking mode by a very extreme time out – like 5 microseconds, and max. chars read is 128.

    tl;dr; essentially this is like processor multi-threading.

    HOWEVER!!! you need sockets if it’s done like this.

    EDIT: adding an example code stripped from all custom actions: (yeah, it’s long)

    set_time_limit(0); // so we don't get a timeout from PHP
    // - can lead to sockets left open...
    
    $config_port = 85; // port
    $config_address = "192.168.0.199"; // IP address, most likely local with router
    // if there's not a router between server and wan, use the wan IP
    $config_to = 30; // global timeout in seconds
    $config_connect_to = 2; // timeout for connecting to remote server
    $config_client_to = 0.1; // timeout for reading client data
    $config_remote_to = 10; // timeout for reading remote data (microseconds)
    $config_remote_stage_to = 15; // timeout for last stage (seconds)
    $config_backlog = 5000; // max backlogs, the more the better (usually)
    
    $parent_sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); // parent socket
    $tmp_res = @socket_bind($parent_sock, $config_address, $config_port);
    if ($tmp_res === false){
        echo "Can't bind socket.";
        exit;
        // address or port in use, for example by Apache
    }
    
    $tmp_res = @socket_listen($parent_sock, $config_backlog);
    if ($tmp_res === false){
        echo "Can't start socket listener.";
        exit;
        // hard to tell what can cause this
    }
    
    socket_set_nonblock($parent_sock); // non-blocking mode
    
    $sockets = array(); // children sockets
    $la = time(); // last activity
    while (time() - $la < $config_to){
        $spawn = @socket_accept($parent_sock); // check for new connection
        if ($spawn !== false){
            $la = time();
    
            $ni = count($sockets);
            $sockets[$ni] = array();
            $sockets[$ni]["handle"] = $spawn;
            $sockets[$ni]["stage"] = 1;
            $sockets[$ni]["la"] = time(); // for some stages
            $sockets[$ni]["client_data"] = "";
            $sockets[$ni]["headers"] = array();
            $sockets[$ni]["remote"] = false;
        }
    
        foreach ($sockets as &$sock){ // &$sock because we're gonna edit the var
            switch ($sock["stage"]){
            case 1: // receive client data
            $read_data = @socket_read($sock["handle"], 2048);
            if ($read_data !== false && $read_data !== ""){
                $la = time();
                $sock["la"] = microtime(true);
                $sock["client_data"] .= $read_data;
            } else if (microtime(true) - $sock["la"] > $config_client_to) {
                // client data received (or too slow :D)
                $sock["stage"] = 2; // go on
            }
            break;
            case 2: // connect to remote
            $headers = explode("\r\n", $sock["client_data"]);
            foreach ($headers as $hdr){
                $h_pos = strpos($hdr, ":");
                if ($h_pos !== false){
                    $nhid = count($sock["headers"]);
                    $sock["headers"][strtolower(substr($hdr, 0, $h_pos))] = ltrim(substr($hdr, $h_pos + 1));
                }
            }
    
            // we'll have to use the "Host" header to know target server
            $sock["remote"] = @fsockopen($sock["headers"]["host"], 80, $sock["errno"], $sock["errstr"], $config_connect_to);
            if ($sock["remote"] === false){
                // error, echo it and close client socket, set stage to 0
                echo "Socket error: #".$sock["errno"].": ".$sock["errstr"]."<br />\n";
                flush(); // immediately show the error
                @socket_close($sock["handle"]);
                $sock["handle"] = 0;
            } else {
                $la = time();
                // okay - connected
                $sock["stage"] = 3;
            }
            break;
            case 3: // send client data
            $tmp_res = @fwrite($sock["remote"], $sock["client_data"]);
            // this currently supports just client data up to 8192 bytes long
            if ($tmp_res === false){
                // error
                echo "Couldn't send client data to remote server!<br />\n";
                flush();
                @socket_close($sock["handle"]);
                @fclose($sock["remote"]);
                $sock["stage"] = 0;
            } else {
                // client data sent
                $la = time();
                stream_set_timeout($sock["remote"], $config_remote_to);
                $sock["la"] = time(); // we'll need this in stage 4
                $sock["stage"] = 4;
            }
            break;
            case 4:
            $remote_read = @fread($sock["remote"], 128);
            if ($remote_read !== false && $remote_read !== ""){
                $la = time();
                $sock["la"] = time();
                @socket_write($sock["handle"], $remote_read);
            } else {
                if (time() - $sock["la"] >= $config_remote_stage_to){
                    echo "Timeout.<br />\n";
                    flush();
                    @socket_close($sock["handle"]);
                    @fclose($sock["remote"]);
                    $sock["stage"] = 0;
                }
            }
            break;
            }
        }
    }
    
    foreach($sockets as $sock){
        @socket_close($sock["handle"]);
        @fclose($sock["remote"]);
    }
    @socket_close($parent_sock);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry if the title is not that specific, but I don't know how else
Sorry the title is not very descriptive but it is a tricky problem to
sorry if the title is very confusing, but I'm not sure how to really
sorry if the title is not very clear. I will try to explain now:
First of all, sorry about that title. I'm not the best at writing those
Sorry that I haven't done much of my own research but I do not
Sorry for the not very descriptive question title. I'm not very sure how to
Firstly, sorry about the question title, it may not be very descriptive for my
Not sure if the title makes sense sorry... basically I'm generating Word documents that
Sorry for the very long title but it's exactly what I'm looking for. I

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.