Page 1 of 1

Noob Question

Posted: Fri Jul 17, 2009 5:26 pm
by MarcoVici
Hi,

Given the file ClientHttpRequest.php (code given below), and given that it will be accessed as such from a web browser from multiple clients:

http://<host name>/ClientHttpRequest.php?url=<some url>&port=443&arg1=somearg&arg2=somearg&arg3=somearg&path=/test.php&svr=<my server>

Is the following ClientHttpRequest code thread safe? (Please note the last line of the code which is instantiating the class and calling the public method in the same line.)

Thanks.

------------------------

Code: Select all

 
<?php
 
class ClientHttpRequest {
   public function __construct() {
   }
 
   public function post() {
      $host = $_GET['url'];
      $port = $_GET['port'];
      $req = "arg1=" . $_GET['arg1'] . "&arg2=" . $_GET['arg2'] . "&arg3=" . $_GET['arg3'];
      $http = "POST " . $_GET['path'] . " HTTP/1.1\r\n";
      $http .= "Host: " . $_GET['svr'] . "\r\n";
      $http .= "User-Agent: " . $_SERVER['HTTP_USER_AGENT'] . "\r\n";
      $http .= "Content-Type: application/x-www-form-urlencoded\r\n";
      $http .= "Content-Length: " . strlen($req) . "\r\n";
      $http .= "Connection: close\r\n\r\n";
      $http .= $req . "\r\n\r\n";
 
      $fp = fsockopen ($host, $port, $errno, $errstr, 30);
 
      if (!$fp) {
         echo "Could not open a socket to host";
      } else {
         // NO HTTP ERROR
         fputs($fp, $http);
 
         // Get final acknowledgement
         while (!feof($fp)) {
            $res .= fgets ($fp, 1024);
         }
         fclose ($fp);
         print $res;
      }
   }
}
 
(new ClientHttpRequest())->post();
?>

Re: Noob Question

Posted: Fri Jul 17, 2009 5:44 pm
by requinix
Why wouldn't it be? This is PHP we're talking about.

Oh, you should probably urlencode those GET values before sticking them into the request.

Re: Noob Question

Posted: Fri Jul 17, 2009 5:47 pm
by MarcoVici
tasairis wrote:Oh, you should probably urlencode those GET values before sticking them into the request.
You're right! Thanks.

Re: Noob Question

Posted: Fri Jul 17, 2009 6:03 pm
by MarcoVici
Just tested it. It doesn't like the last line. I had to break it into two lines:

$chr = new ClientHttpRequest();
$chr->post();

Given that, is this still thread safe? Something tells me it isn't.

Re: Noob Question

Posted: Fri Jul 17, 2009 7:30 pm
by requinix
MarcoVici wrote:Given that, is this still thread safe? Something tells me it isn't.
Again, why wouldn't it be? There is nothing in that code that could influence another running copy.

Re: Noob Question

Posted: Fri Jul 17, 2009 7:53 pm
by MarcoVici
tasairis wrote:
MarcoVici wrote:Given that, is this still thread safe? Something tells me it isn't.
Again, why wouldn't it be? There is nothing in that code that could influence another running copy.
If I knew, I wouldn't be asking :-) I was under the assumption that PHP works like Java Servlets in that there is only one instance that always hangs around serving all requests. The $chr variable, being outside of any class and function, kinda concerned me. But you're saying a new php instance is created for every request?

Re: Noob Question

Posted: Fri Jul 17, 2009 9:49 pm
by requinix
Pretty much, yes.

It's not so much a "new PHP instance" but PHP running a "new script". They're all separate from each other.