LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-07-2007, 07:14 PM   #1
nathacof
Member
 
Registered: Aug 2004
Location: Bear, DE, USA
Distribution: Slackware 11, CentOS 5.2, Ubuntu
Posts: 124

Rep: Reputation: 17
PHP pfsockopen


Does anyone know of a good tutorial on using pfsockopen across multiple page loads?

http://dev.analext.com/socket_connect.phps

This seems to be the problem.
PHP Code:
if (!isset($_SESSION['socket'])){
  
$socket pfsockopen($host$port$errno$errstr10);
  
$_SESSION['socket'] = $socket;
} else {
$socket $_SESSION['socket'];} 
The variable $_SESSION['socket'] is getting set but it does not contain a valid file descriptor on subsequent page loads.

Could anyone see a reason why this shouldn't work?
 
Old 05-07-2007, 10:03 PM   #2
nathacof
Member
 
Registered: Aug 2004
Location: Bear, DE, USA
Distribution: Slackware 11, CentOS 5.2, Ubuntu
Posts: 124

Original Poster
Rep: Reputation: 17
Basically I don't think this is possible. PHP is running as a module, however the persistent connection is associated with a specific process. Since Apache is multi-threaded in nature it's very likely that the connection is not available in the process I'm running my script in.

Without completely reconfiguring Apache on my dev server I don't know. Has any one had any experience with this in the past? I've updated my code and I believe everything should be working at this point.

 
Old 02-05-2009, 04:22 AM   #3
masterrahul
LQ Newbie
 
Registered: Feb 2009
Posts: 2

Rep: Reputation: 0
Hello nathacof

I am also facing the same problem; have you found any possible solution. I don't want to create socket again and again.
You can reply me at my mail
mastermailbox2001-manager@yahoo.com
 
Old 02-05-2009, 07:58 AM   #4
nathacof
Member
 
Registered: Aug 2004
Location: Bear, DE, USA
Distribution: Slackware 11, CentOS 5.2, Ubuntu
Posts: 124

Original Poster
Rep: Reputation: 17
Masterrahul,

Unfortunately there is no way to maintain the same socket over multiple requests when running in a webserver context. If your application is running from the command line there would be no issue because the script would maintain the socket as long as it is executing, or you specifically kill it.

Sorry :\
 
Old 02-06-2009, 10:00 AM   #5
masterrahul
LQ Newbie
 
Registered: Feb 2009
Posts: 2

Rep: Reputation: 0
Talking

Quote:
Originally Posted by nathacof View Post
Masterrahul,

Unfortunately there is no way to maintain the same socket over multiple requests when running in a webserver context. If your application is running from the command line there would be no issue because the script would maintain the socket as long as it is executing, or you specifically kill it.

Sorry :\
Yes Now I understand, I changed my application to work in that way.
 
Old 02-06-2009, 07:20 PM   #6
xhypno
Member
 
Registered: Sep 2004
Posts: 62

Rep: Reputation: 16
Quote:
Originally Posted by nathacof View Post
Masterrahul,

Unfortunately there is no way to maintain the same socket over multiple requests when running in a webserver context. If your application is running from the command line there would be no issue because the script would maintain the socket as long as it is executing, or you specifically kill it.

Sorry :\
This is wrong.

pfsock is a persistant socket the same way pconnect works for a mysql connection.

The dev resource is opened and when ever you call pfsock again it will call the same dev resource until the interpreter runs its gc which only takes things not used after 30 min. So yes it seems that you will be opening a new sock every time, but using pfsock it uses the smae resource when opened.

From PHP.net
"This function behaves exactly as fsockopen() with the difference that the connection is not closed after the script finishes. It is the persistent version of fsockopen()."


This would do the trick at the top of each script or the one opened over and over.

Code:
if(!socket){
    $socket = pfsockopen($host, $port, $errno, $errstr, 10);
}
 
Old 02-06-2009, 07:26 PM   #7
nathacof
Member
 
Registered: Aug 2004
Location: Bear, DE, USA
Distribution: Slackware 11, CentOS 5.2, Ubuntu
Posts: 124

Original Poster
Rep: Reputation: 17
xhypno, have you read the comments on, http://php.net/pfsockopen/. I think you'll find it's a bit more complicated than you assume depending on how PHP is being executed. IE: ISAPI Module, PHP as a CGI, Apache Prefork or Worker MPM, all have different behavior in this regards.
 
Old 02-07-2009, 11:00 AM   #8
xhypno
Member
 
Registered: Sep 2004
Posts: 62

Rep: Reputation: 16
Quote:
Originally Posted by nathacof View Post
xhypno, have you read the comments on, http://php.net/pfsockopen/. I think you'll find it's a bit more complicated than you assume depending on how PHP is being executed. IE: ISAPI Module, PHP as a CGI, Apache Prefork or Worker MPM, all have different behavior in this regards.
I have actually used it a lot. The only time it will be complicated is with Apache Prefork as each page call is in a new fork of apache and a new php interpreter. But the chances you are doing it in prefork is very slim because you only see that in large scale shared hosting environments that have been poorly designed where each user forks their own apache and php instance each time a page is accessed instead of chrooting them to one environment. You see this in a few of the Affinity/Hostway hosting services but that is about it because it is a very poor practice.
 
Old 09-16-2009, 05:19 AM   #9
troykelly
LQ Newbie
 
Registered: Sep 2009
Posts: 1

Rep: Reputation: 0
It seems that when running php in almost any fashion on a web server - pfsockopen serves no purpose.

Each time the script is run, a new socket is instantiated.

It's fairly easy to test... run it - and see how many connections you have after a few refreshes.

xhypno - I would love to see an example of how you are running it so the sockets are re-used.

To be honest, I think it would be a bit of a security issue in a webserver environment anyway. How would you identify yourself as the rightful owner of the socket?
 
Old 09-16-2009, 08:26 AM   #10
nathacof
Member
 
Registered: Aug 2004
Location: Bear, DE, USA
Distribution: Slackware 11, CentOS 5.2, Ubuntu
Posts: 124

Original Poster
Rep: Reputation: 17
Quote:
Originally Posted by troykelly View Post
It seems that when running php in almost any fashion on a web server - pfsockopen serves no purpose.
It depends entirely on the SAPI in use by your version of PHP.
  • If you're using a thread safe version of PHP sockets should be available to re-use. (IIS, Apache Worker MPM)
  • With Apache's Pre-Fork MPM each httpd process only has access to it's own pool of resources, and each request may not be served by the same process.
  • With CGI you go through the whole PHP init, and shutdown routines every time you load a page, so there is no affect.
  • Using FastCGI however, should allow you to share persistent connections across all of your scripts.

Reference: http://devzone.zend.com/article/1021#Heading3

I'm not sure how this is a security concern. It's not like someone can take over your socket when it's in use. PHP just doesn't tear down the connection when you're done, so the person obtaining the persistent socket has no idea whether it was persistent or a brand new connection.

Last edited by nathacof; 09-16-2009 at 10:12 AM.
 
1 members found this post helpful.
Old 02-29-2012, 03:00 AM   #11
golden_boy615
Member
 
Registered: Dec 2008
Distribution: Ubuntu Fedora
Posts: 442

Rep: Reputation: 17
thank you nathacof beracuse of your good illustration I have the same code like you with pfsockopen and no problem in apache I mean for each web page or session that opens on different client I have individual persistent socket and it does not close and everything goes fine and every session has its own socket but with on lighthttpd and fastcgi enable, my persistent socket will share for all web pages I mean all of different sessions have just one persistent socket .
as you said :
Quote:
Using FastCGI however, should allow you to share persistent connections across all of your scripts
I do not want fastcgi to share persistent connections across all of my sessions and I want each session has its own persistent socket.
 
  


Reply

Tags
load, page, persistant, php, servers, session, socket, web


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Are PHP session variables held in memory when you leave a PHP site? Locura Programming 11 11-16-2008 08:37 PM
Adding users with PHP (pass php variables to Expect script) Jayla Programming 1 10-20-2006 10:44 AM
Php account verify email links back to php.org zenerdiode Linux - Server 3 10-03-2006 05:21 PM
php5 apache2 mysql4 don't work, php does not seem to read php.ini atom Linux - Software 5 03-24-2005 11:05 AM
php apache or php cgi - php learner rblampain Linux - Security 3 12-17-2004 11:10 PM


All times are GMT -5. The time now is 11:05 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration