tags:

views:

30

answers:

2

Hello there,

Is it possible to create a http response for requests associated with a different PHP session? If so, how to do that?

I'm creating a script language to make it easier for PHP developers to handle phone interactions. My application receives phone calls and then activates the user scripts associated with those calls.

Scripts are processed in real time. Since I don't know the number of commands in the scripts, I have to create and send individual REST responses for each command until somebody issues a hangup.

Is there any way to do that without having to stop the current function, send the response, and then resume the script the next time the phone server sends me a request?

Ideally, I would love to remain in the current PHP function sending responses for each http request without having to stop at each time... would curl -- or anything else -- help me with that?

Thanks in advance,

Leo

A: 

I hope i got right what you're trying to do,
well, if you have the desired other session's id, just use session_id() function to set which session actually used is used, for example:

#Assuming url like www.example.com/?session_id=123456;
$current_session_id = session_id();
$desired_session_id = $_GET['session_id'];

session_id($desired_session_id); #that's where we switch to the desired session
var_dump($_SESSION); #will dump session with id 123456
session_id($current_session_id); #switching back to the previous session id

This should work, I believe that I used something like this in the past, but double check me on that.

cypher
Hello cypher, thanks for your response.The idea of calling session_id() is interesting. But how to create and send an http response for the other session without having to exit the current function I'm in? Would I have to use fork? Would I have to use curl?Thanks once again,Leo
A: 

You can use multi-curl to send multiple requests, but it seems like you need to send the commands in order. Another option would be to use forking, which creates a copy of the current process. You can check if you are the "parent" or "child" process. The "parent" process then monitors the "child" processes. I've done this with up 50 child processes running at once off of 1 parent process. http://php.net/manual/en/function.pcntl-fork.php

If you just want to send the command, and don't care about the output, just use the exec function. In the example below, PHP won't wait for a response or for the script to complete.

exec("send_command.php > /dev/null &");
Brent Baisley
Hello Brent, thanks for your message. The usage of fork() looks interesting. However, according to the PHP manual (http://www.php.net/manual/en/intro.pcntl.php):* "Process Control should not be enabled within a web server environment"* "This extension (PCNTL) is not available on Windows platforms""exec" also looks a little dangerous and platform-dependent. Would there be other alternatives?For instance, would it be possible to use curl to create a response for another session?Thanks,Leo
correct, pcntl should not be used under Apache or other web service. You generally use it under command line PHP (ie exec). All my suggestions were assuming a Unix environment. "exec" would run under the user account that calls it, so it has built-in limitations. It's just executing a system command, in this case a script you create. Not really dangerous, unless you pass it arbitrary parameters.
Brent Baisley