Solved

GET,POST, and servers

Posted on 2004-10-04
15
600 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 7

Expert Comment

by:jdpipe
Comment Utility
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__
Comment Utility
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
Comment Utility
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__
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:__PX__
Comment Utility
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__
Comment Utility
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
Comment Utility
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
Comment Utility

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
Comment Utility
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__
Comment Utility
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
Comment Utility
... thanks for this thread, very useful!

So, how do you send the POST variables?

David
0
 

Author Comment

by:__PX__
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now