• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 657
  • Last Modified:

GET,POST, and servers

Hello.  I recently started building a small web server program in Java.  I've added PHP support to it, but I can't seem to figure out to take the GET and POST variables parsed from HTTP requests and "send them" to php.exe.  Is there a command line option or something that I'm not seeing?

Thanks.
0
__PX__
Asked:
__PX__
  • 6
  • 5
  • 2
  • +2
1 Solution
 
php-webdesignCommented:
so your $_POST['fieldname'] isn't working?

$_GET and $_POST can only be called when you submit an HTML form

<form action="test.php" method="POST">
<input type="text" value="" name="testvar"></input>
<input type="submit" value="submit">
</form>

when you would submit this form, the var would be:

$_POST['testvar'];
0
 
jdpipeCommented:
You've got to configure PHP to run as a CGI program, or else as an Apache Module, depends what HTTP server you're running (maybe Apache, IIS, Netscape, or others).

Probably you need to go through the installation instructions for php, see

http://au2.php.net/manual/en/install.windows.apache1.php

Hope that helps
JP
0
 
ThGCommented:
So you made *the webserver*. You need to pass a lot of information to the php executable (building it as CGI interface will help definitely, but it's not needed). You can do  this using ENVIRONMENT variables. Read them here:

http://hoohoo.ncsa.uiuc.edu/cgi/interface.html
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
jdpipeCommented:
Good tip, ThG. I missed that in the question from PX. Looking at that page you sent, it looks like setting up a CGI interface isn't too hard: you have to set all those environment variables like REQUEST_URI, QUERY_STRING etc. Probably you don't need to use all of them right from the start, probably just using those two would be a good start.
0
 
__PX__Author Commented:
Environment Variables were my first guess actually.  But java has gotten rid of getEnv() and setEnv() and there's no direct access to env. variables anymore.  I found out I can send GET variables through the command line, but I still have to look for a way to set the environment variables.
0
 
ThGCommented:
There is no reason Java would stop you from setting environment variables. You probably can't set them for the running application, but since you spawn a new child process (the php executable), you must be able to interact with ITS environment (yes, a new child process has its own environment).

This became definitely a Java question now :-)
0
 
__PX__Author Commented:
I've already tried sending the environment variables to the child process several times, but that triggers an error from php's "force cgi redirect".  If I turn that off,it doesn't recognize the file even though it does recognize it when I dont send it an environment.

What I've decided to do is use JNI to compile a library that uses C's getenv() and putenv().  Not a solution that works on all OS's, but it'll have to do.
0
 
__PX__Author Commented:
Actually this causes the same problem.  Back to the PHP side of the topic, what exactly does force cgi redirrect do?  I know its a security feature so I can't see how its useful because as soon as you set a PHP environment variables (by putenv() or sending the environment to the process) ? And yet, when I turn it off and try to send an environment variable (again by either mehtod), PHP says "Input File Found".
0
 
__PX__Author Commented:
That should have said "No input file specified."  Don't ask me why I wrote "Input File Found" because I have no idea.

But I fixed it.  I had forgotten to set one of the environment variables and the script would not run correctly until it was added.
0
 
ThGCommented:
Well one thing is the environment variables, one thing is the input script. You can run it using php -f /path/to/script.php or piping it inside php. (i.e. popen("/usr/bin/php", "w") and writing inside this stream).

What about this forge cgi redirect problem?
0
 
ThGCommented:

OK i checked out the PHP sources. You can disable this force-redirect thing from your config. Anyway looks like you didn't read the URL i pointed you at. Please DO.

This is the applying snipped code: You have to set REDIRECT_STATUS env var.

                if (!getenv("REDIRECT_STATUS")
                        && !getenv ("HTTP_REDIRECT_STATUS")
                        /* this is to allow a different env var to be configured
                            in case some server does something different than above */
                        && (!redirect_status_env || !getenv(redirect_status_env))
                        ) {
                        SG(sapi_headers).http_response_code = 400;
                        PUTS("<b>Security Alert!</b> The PHP CGI cannot be accessed directly.\n\n\
0
 
ThGCommented:
If you want to see the complete list of env variables set by a real server, create a file in cgi-bin like this

#!/bin/sh

echo -en "Content-Type: text/plain\n\n"
set

----

chmod it 755 and run it from your browser.
0
 
__PX__Author Commented:
I had read that page many times since you posted it.  It helped me find the env. variable I needed to to fix the "No Input File Specified" problem (SCRIPT_FILENAME I believe).  And its also helped me realize how to send GET and POST variables (took me a while to realize it with the POST variables).  For the record, REDIRECT_STATUS is not mentioned on that page, but I've found out what it does and set it to the right value (200).

Anyway, my original question has been answered and I have no more questions.
0
 
davidlandyCommented:
... thanks for this thread, very useful!

So, how do you send the POST variables?

David
0
 
__PX__Author Commented:
Sorry David.  I forgot to come back and answer your question.  To send the POST data, you take the POST data from the request and send it to PHP's input stream.  My Java code to open PHP and send the necessary POST data is:

Process php = null;

// data is a char[] filled with the POST data
if(data != null)
{
    // config is a custom class that holds the environment variables in a hash table
    php = Runtime.getRuntime().exec("C:\\php-4.3.9-Win32\\php", config.getEnvironment());
                                    
    PrintWriter fOutput = new PrintWriter( php.getOutputStream());
    fOutput.println(data);
    fOutput.close();
}

I hope this helps someone as much as it helped me to figure it out.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 5
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now