Solved

GET,POST, and servers

Posted on 2004-10-04
15
628 Views
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?

Thanks.
0
Comment
Question by:__PX__
  • 6
  • 5
  • 2
  • +2
15 Comments
 
LVL 5

Expert Comment

by:php-webdesign
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">
</form>

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

$_POST['testvar'];
0
 
LVL 7

Expert Comment

by:jdpipe
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

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

Hope that helps
JP
0
 
LVL 14

Accepted Solution

by:
ThG earned 75 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:

http://hoohoo.ncsa.uiuc.edu/cgi/interface.html
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 7

Expert Comment

by:jdpipe
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.
0
 

Author Comment

by:__PX__
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.
0
 
LVL 14

Expert Comment

by:ThG
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 :-)
0
 

Author Comment

by:__PX__
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.
0
 

Author Comment

by:__PX__
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".
0
 

Author Comment

by:__PX__
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.
0
 
LVL 14

Expert Comment

by:ThG
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?
0
 
LVL 14

Expert Comment

by:ThG
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\
0
 
LVL 14

Expert Comment

by:ThG
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

#!/bin/sh

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

----

chmod it 755 and run it from your browser.
0
 

Author Comment

by:__PX__
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.
0
 

Expert Comment

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

So, how do you send the POST variables?

David
0
 

Author Comment

by:__PX__
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());
    fOutput.println(data);
    fOutput.close();
}

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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

828 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