Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


GET,POST, and servers

Posted on 2004-10-04
Medium Priority
Last Modified: 2008-01-16
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?

Question by:__PX__
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 2
  • +2

Expert Comment

ID: 12224647
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">

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


Expert Comment

ID: 12224770
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


Hope that helps
LVL 14

Accepted Solution

ThG earned 225 total points
ID: 12228254
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:

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 12233008
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.

Author Comment

ID: 12233117
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.
LVL 14

Expert Comment

ID: 12238536
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 :-)

Author Comment

ID: 12239925
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.

Author Comment

ID: 12240173
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".

Author Comment

ID: 12242429
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.
LVL 14

Expert Comment

ID: 12244142
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?
LVL 14

Expert Comment

ID: 12244188

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\
LVL 14

Expert Comment

ID: 12244201
If you want to see the complete list of env variables set by a real server, create a file in cgi-bin like this


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


chmod it 755 and run it from your browser.

Author Comment

ID: 12245975
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.

Expert Comment

ID: 14113594
... thanks for this thread, very useful!

So, how do you send the POST variables?


Author Comment

ID: 14646687
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());

I hope this helps someone as much as it helped me to figure it out.

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question