Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Network Functions : pfsockopen

pfsockopen

Open persistent Internet or Unix domain socket connection (PHP 4, PHP 5)
resource pfsockopen ( string hostname [, int port [, int &errno [, string &errstr [, float timeout]]]] )


Code Examples / Notes » pfsockopen

dietrich

Re: persisting connections across script executions
SAPI modules allow this (i've only tried w/ ISAPI, but others might also allow).
also, Shane Caraveo's FastCGI allows this.


grey

pfsockopen is useful for having scripts executing others in the background.
I'm writing an intra-site search engine with spidering capabilities.  To spider a ~200 page site and index all the text takes about 60 seconds.  Because I don't want the user waiting 60s for the script to finish and thinking it's timing out, I use pfsockopen to have the server open an HTTP connection with itself to execute the script in the background.  Once the spidering is done, the connection is closed without capturing the HTTP response.  The output isn't important, the spidering is!
I just make the script execute the following:
<?php
$st = pfsockopen($_SERVER['HTTP_HOST'], 80, $erstr, $errno, 5);
fwrite($st, "GET {$_SERVER['PHP_SELF']}?$key HTTP/1.0\r\nHost: {$_SERVER['HTTP_HOST']}\r\n\r\n");
?>
Where $key is the QUERY_STRING trigger which starts the spidering process.  This script is executed in the background while the main script is free to finish loading and display a message like: "The database is being reindexed, please wait."
BTW, I set a flag in another MySQL table which prevents searches while the spider is crawling.


pulstar

Persistent connections either in socket or databases should be used only in servers where the limits are well defined. For example, the number of allowed connections in a database must be greater than the number of Apache's processes, or the connections will be refused by the database (this will surely occur if you use persistent connections). The same may occur with socket connections. This is up to the service configuration. In my opinion, persistent connections are useful only if you have total control over the one or more servers involved, like on a heavy loaded dedicated server for example, where the little gain in performance worth the use of such connections. Never use them in a shared server.

php dot net

OK, WRT to the p* functions opening a new connection when one already exists. It is my understanting that (under Apache anyways) this is on a per-process basis. If you do a 'ps auxw|grep httpd' on your server you will see more than one process. What p* does is make a p-connection on one of those processes only, the one that actually handles your request. Chances are that when you hit the page again it will be answered by a different process. I'm guessing if you keep hitting reload you'll get around to the original process again and there will be no error message or second connection open. Anyhow, this is true of all p* functions; they open not one connection per server, but one connection per server _process_.

ludvig dot ericson

Confusion arises as to when PHP starts a new connection using all the
"persistent" versions of any function, and this depends entirely on how you
run your PHP.
In real CGI mode, that is, one process per script, persistent functions do the
exact same as their "temporary" equivalents. If you have a threaded Apache
MPM, this persistence will open a connection per thread, but not immediately.
Think of it as a single PHP instance for each thread.
If you run prefork, the MPM that forks the Apache server into several
accept()ing subprocesses, you'll have one PHP instance per process.
This isn't always true as I might've missed some gotchas, but in general, do
know that a persistent can only try to be persistent.
As for grey at greywyvern dot moc: A cronjob would be a lot better suited
for this, and have it periodically update the index rather than request ~200
pages each time somebody searches, at least from what you describe it as.


Change Language


Follow Navioo On Twitter
checkdnsrr
closelog
debugger_off
debugger_on
define_syslog_variables
dns_check_record
dns_get_mx
dns_get_record
fsockopen
gethostbyaddr
gethostbyname
gethostbynamel
getmxrr
getprotobyname
getprotobynumber
getservbyname
getservbyport
header
headers_list
headers_sent
inet_ntop
inet_pton
ip2long
long2ip
openlog
pfsockopen
setcookie
setrawcookie
socket_get_status
socket_set_blocking
socket_set_timeout
syslog
eXTReMe Tracker