Really Tricky Question: Trust Me

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

BIGjuevos
Forum Newbie
Posts: 4
Joined: Tue Jan 02, 2007 3:20 pm

Really Tricky Question: Trust Me

Post by BIGjuevos »

How does one save a resource(such as a socket) within a class based object from one script for use within another script which is later executed.

OK, before you start asking me questions, let me state what I know:
-The PHP website says that you cannot store a resource in a session, which I tried and found out the hard way.
-You cannot serialize a resource and store it on the filesystem, tried that too.
-I don't know how to do binary operations on the filesystem with PHP.

Possible Solutions, which I don't know how to implement
-Could I save the class/resource to the file system in it's entirety and be restored later.

OK, that's all I have. I have faith in you gurus. So please help this intermediate user out!

Thanks in advance,
Ryan
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

BIGjuevos wrote:Possible Solutions, which I don't know how to implement
-Could I save the class/resource to the file system in it's entirety and be restored later.
No, you cannot.
BIGjuevos
Forum Newbie
Posts: 4
Joined: Tue Jan 02, 2007 3:20 pm

Post by BIGjuevos »

volka wrote:
BIGjuevos wrote:Possible Solutions, which I don't know how to implement
-Could I save the class/resource to the file system in it's entirety and be restored later.
No, you cannot.
Any suggestios?

I am completely open.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

You have to store something else than a resource, e.g. host, port and protocoll (as strings and numbers) to re-create the socket.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

BIGjuevos wrote:I am completely open.
Maybe you could open a socket in UNIX and read/write to it from PHP?

You could even create the socket from within PHP if you have permissions.
BIGjuevos
Forum Newbie
Posts: 4
Joined: Tue Jan 02, 2007 3:20 pm

Post by BIGjuevos »

That's the problem, I'm not looking to re-connect, I am looking to "re-open" the socket seconds later in another script.
BIGjuevos
Forum Newbie
Posts: 4
Joined: Tue Jan 02, 2007 3:20 pm

Post by BIGjuevos »

Kieran Huggins wrote:
BIGjuevos wrote:I am completely open.
Maybe you could open a socket in UNIX and read/write to it from PHP?

You could even create the socket from within PHP if you have permissions.
But how do I transfer the resource from the script that ended that created the socket to the script that is now running that want to use that same open socket?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Everything in UNIX (including sockets) are treated as files.

I would use a session variable to point to the existing open socket "file". Then a quick fopen('/path/to/socket.file'); would give you access to your existing socket.

You may want to include a unique variable in the filename of the socket file to keep concurrent sessions from colliding.

Read http://www.cs.rpi.edu/courses/sysprog/sockets/sock.html for more info on sockets in UNIX

(upon further reading, this seems to be a C reference)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

So you are wanting to use a resource created in one script to be used in another, unrelated script that executes after the creating script has already terminated?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Kieran Huggins wrote:Everything in UNIX (including sockets) are treated as files.

I would use a session variable to point to the existing open socket "file". Then a quick fopen('/path/to/socket.file'); would give you access to your existing socket.
That's only true for unix sockets, not for at_inet
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

volka wrote:That's only true for unix sockets, not for at_inet
I have NO experience doing this - trust volka!
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Sounds like you want a persistent connection.
Is pfsockopen any use?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

ole! Ouch!

RTQ or RTFM?

In defense of RTQ ... there is a Socket section in the manual and then there are five socket functions in the Network section.
(#10850)
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Huh? :?

OK I'm guessing RTQ stands for "read the question".
Based on this
That's the problem, I'm not looking to re-connect, I am looking to "re-open" the socket seconds later in another script.
I suggested persistent connections. If that was incredibly stupid suggestion for some reason...well I've never used sockets before and please educate me :)
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

I have no idea what RTQ is either... but you totally gave us all a swift kick on that one ;-) I had no idea there was a pfsockopen()!

Good to know! 8)
Post Reply