I would like to start a PHP script from a bash CGI script in such a way that the PHP script can access the same session, i.e., the cookies. Is this possible?
Asked
Active
Viewed 1,074 times
1
-
If you know session id you could just specify `session_id('id here');` right before `session_start()` – zerkms Jan 16 '12 at 22:07
-
2Did you invoke the `php-cgi` binary? That should inherit the CGI environment and accept the `HTTP_COOKIE` env variable. – mario Jan 16 '12 at 22:15
-
@zerkms I tried that. According to the [php manual](http://php.net/manual/en/function.session-id.php) setting session_id creates new cookies even if it exists. – Jamie Kitson Jan 16 '12 at 23:08
-
@mario That sounds good, but it's behaving very strangely, it's almost as if it's cating the files it's being run in rather running the named php script. – Jamie Kitson Jan 16 '12 at 23:31
-
You need to adapt SCRIPT_FILENAME of course. – mario Jan 16 '12 at 23:35
-
@mario Thanks, if you want to put that as an answer I'll accept it. There isn't an option to skip the HTTP header is there? – Jamie Kitson Jan 17 '12 at 10:52
2 Answers
1
You can use the php-cgi
binary. If invoked from another CGI script it inherits the environment, thus all HTTP_*
variables, including the cookies. There are a few setup caveats:
SCRIPT_FILENAME
needs to be adapted to the PHP script. Thephp-cgi
binary ignores the file argument otherwise.- Likewise adapt
SCRIPT_NAME
and eventuallyREQUEST_URI
- You also need
REDIRECT_STATUS=200
depending on config. - This works for GET rquests, repiping POST data is often an issue.
To suppress the php-cgi header output, you can possibly invoke it with -q
however. That shouldn't impair the CGI input, just the response.
-
The php-cgi call from my bash cgi script does not seem to be processing any parameters I give it (eg, -q). Any ideas? – Jamie Kitson Jan 18 '12 at 22:13
-
It's possible that it ignores it for later php-cgi versions and when a full CGI environment is present. In that case you need to post-filter it. (There is also a `cgi.nph` setting, but I doubt it would help. I can't really locate either in the cgi_main.c source) – mario Jan 18 '12 at 22:22
-
0
Curl can do it, although via apache rather than directly:
curl -sb "$HTTP_COOKIE" http://example.com/script.php
Although at the moment it doesn't seem to be reproducing carriage returns.

Jamie Kitson
- 3,973
- 4
- 37
- 50