Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP from the command line (post and get)

Status
Not open for further replies.

BB101

Programmer
May 23, 2001
337
GB
I have succesfully made/converted/hacked a simple VB web server to run CGI scripts (PHP from the command line). However, I cannot get the GET or POST vars into PHP and this is causing me much grief.

Please someone recommend me a method to doing this. --BB
 
When a web server hands data in a GET-method form to a CGI, it does so via an environment variable. The variable QUERY_STRING contains everything after the question mark.

POST-method data is provided to a CGI via STDIN. The server must set the environment variable CONTENT_LENGTH to the number of bytes that will be available on STDIN.

Does this help at all?
 
yes very much so...

is there a more detailed tutorial? or can you show me exactly what you mean, I am fimiliar with using php_cli with STDIN and STDOUT calls --BB
 
Unfortunately, I don't know of a tutorial, and all my down and dirty CGI programming experience was with c-language compiled program.

Given the form:
<html><body>
<form method=get action=foo.php>
<input type=text name=texting>
<input type=text name=texttoo>
<input type=submit>
</form>
</body></html>

The actual URL you go to is
The server must strip off everything after the question mark and put that data, as is, into the environment variable QUERY_STRING then invokes the script. In the current case, the variable QUERY_STRING contains &quot;texting=a&texttoo=b&quot;. The CGI then parses it from there.

When accepting POST action form data

<html><body>
<form method=post action=foo.php>
<input type=text name=texting>
<input type=text name=texttoo>
<input type=submit>
</form>
</body></html>

When you hit the submit button, the browser sends the data back in the HTTP stream it sends to the server. The data sent from the server will look something like:

POST /foo.php HTTP/1.0
Content-type: application/x-Content-length: 24
< a blank line >
texting=a
&texttoo=b

The server then has to make all the data after that blank line available to the CGI via STDIN for it to parse. I don't know if the &quot;Content-length:&quot; header includes [CR][LF] pairs in its count or not. I also don't know whether the data comes one line to a field or not.

What you want to do is to create an HTML page with a POST method form that sends data to your server. You should then be able to view all data and HTTP headers sent in the stream by the browser.
 
thanks, that is a great help! :) --BB
 
There is also a third variation, the one used by forms where a file upload is necessary. The method of the form is POST and and additional attribute of the FORM tag, enctyp, is set to the value &quot;multipart/form-data&quot;.

When this happens, the data comes in with the HTTP headers like it does with any other POST method input. However, the data is MIME encoded.
 
Where and how do cookies go? --BB
 
When a server wants to set a cookie, it sends the HTTP header &quot;Set-Cookie&quot; with all the particulars as to expiry, domain the cookie is available and server path to which the cookie is available. When a browser needs to report a known cookie, it uses the &quot;Cookie&quot; HTTP header.

Cookies were invented by Netscape. Their document specifying how they should canonically work can be found at:
 
in relation to PHP? how do I get the cookies into the CGI call? --BB
 
All cookie information should be available to the CGI through the environment variable &quot;HTTP_COOKIE&quot; Perfection in engineering does not happen when there is nothing more to add. Rather it happens when there is nothing more to take away.
 
I realise there is more to it and need to get a specification of the CGI (PHP specific or not), where all the variables are stored and how they are presented.

Anyone? --BB
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top