top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Pass variable to pcntl_fork child in PHP

0 votes
489 views

I've tried the following code but the $call variable is not available in the pcntl_fork child. Searching the web, I've found posts that claim that all variables defined before pcntl_fork are copied to the child. I've also found posts that claim that no variables are passed/copied to the child. So, which of them is true and how should I correct my code to make it work?

 $call = str_replace('@@', '&', $_GET['call']);

 if ($pid = pcntl_fork())
 {
     $previousCalls=file("calls.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
     if (!in_array($call, $previousCalls))
     {
         file_put_contents("calls.txt", $call."\n", FILE_APPEND);
     }
 }
 else
 {
     function displayUrl($url)
     {
         $ch = curl_init($url);
         curl_setopt ( $ch, CURLOPT_HEADER, 1);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
         $curlResult = curl_exec($ch);
         curl_close($ch);
         list($headers,$content) = explode("\r\n\r\n",$curlResult,2);
         foreach (explode("\r\n",$headers) as $hdr)
         {
             if ($hdr != 'Transfer-Encoding: chunked')
             {
                 header($hdr);
             }
         }
         echo $content;
     }
     displayUrl($call);
 }
posted Jul 2, 2014 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Child process should has the same environment as the parent process. I
don't see any thing wrong with your code.
I assume that you are using php to handle http request for the using of
$_GET , but I think it is wrong to use multi process in PHP in the HTTP
SERVER environment

answer Jul 9, 2014 by Vrije Mani Upadhyay
Similar Questions
+1 vote

I am trying to pass a "PHP variable" from Apache (2.2.15) to Tomcat (7.0.27) but without success. The apache is used as front-end server of tomcat using mod_proxy_ajp.

I verified that I can pass only variable set by Apache (e.g. using SetEnv directive); How I can pass a variable set by PHP to Tomcat?

+1 vote

I have just started learning PHP. So Can anyone tell me that How many methods are there to pass the variable through navigation between the different pages ?

+1 vote

Why I can't read static variables of a child class, even so the variable is protected or public?

Here's what I tried:

class A {
 protected static $foo = "bar";
}

class B {
 public function readFoo() {
 return self::$foo;
 }
}

When now calling B::readFoo, I get the following error:

var_dump((new B())->readFoo());

PHP Fatal error: Access to undeclared static property: B::$foo in php shell code on line 1
PHP Stack trace:
PHP 1. {main}() php shell code:0
PHP 2. B->readFoo() php shell code:1

Fatal error: Access to undeclared static property: B::$foo in php shell
code on line 1

Call Stack:
 86.2643 231224 1. {main}() php shell code:0
 86.2666 231352 2. B->readFoo() php shell code:1
0 votes

I would like to know how can I pass a parameter via URL using control value on the form.
Something like myPage.php?MyID=txtMyID.value
I can use myPage.php?MyID=1, but cannot use myPage.php?MyID=txtMyID.value.

...