PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
MarcoVici
Forum Newbie
Posts: 4 Joined: Fri Jul 17, 2009 5:25 pm
Post
by MarcoVici » Fri Jul 17, 2009 5:26 pm
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();
?>
Last edited by
Benjamin on Fri Jul 17, 2009 9:38 pm, edited 1 time in total.
Reason: Added [code=php] tags.
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Fri Jul 17, 2009 5:44 pm
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.
MarcoVici
Forum Newbie
Posts: 4 Joined: Fri Jul 17, 2009 5:25 pm
Post
by MarcoVici » Fri Jul 17, 2009 5:47 pm
tasairis wrote: Oh, you should probably
urlencode those GET values before sticking them into the request.
You're right! Thanks.
MarcoVici
Forum Newbie
Posts: 4 Joined: Fri Jul 17, 2009 5:25 pm
Post
by MarcoVici » Fri Jul 17, 2009 6:03 pm
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.
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Fri Jul 17, 2009 7:30 pm
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.
MarcoVici
Forum Newbie
Posts: 4 Joined: Fri Jul 17, 2009 5:25 pm
Post
by MarcoVici » Fri Jul 17, 2009 7:53 pm
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?
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Fri Jul 17, 2009 9:49 pm
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.