Programming question

Get answers here.

Moderators: BBear, theunknownhost, flaguy

Post Reply
User avatar
Sapphyre
Official Test Penquin
Posts: 3337
Joined: Tue Nov 19, 2002 2:50 am
Location: Canada

Programming question

Post by Sapphyre » Wed Nov 08, 2006 9:28 pm

How can you determine if the input a script is receiving is coming in via a form POST vs. STDIN (such as an email message being piped in)

i.e. the same script needs to do something different depending on how it's being run. Suggestions?

User avatar
theunknownhost
Official Test Penquin
Posts: 2713
Joined: Mon Aug 05, 2002 1:05 pm

Post by theunknownhost » Wed Nov 08, 2006 9:45 pm

I would place a hidden variable in the form.

i.e.

Code: Select all

<input type=hidden name=myvar value=5>


Then tell the program to check for the value of $myvar - Logic

Code: Select all

if ($myvar=='5'){
      this was submitted using the form
}
else{
it must have been piped in
}


m2
Hard Drive Crasher
Posts: 767
Joined: Sun Nov 21, 2004 2:19 pm
Location: Lexington, KY

Post by m2 » Wed Nov 08, 2006 10:16 pm

Can you tell from the environment variable REQUEST_METHOD?

sixpackmx
Hard Drive Crasher
Posts: 640
Joined: Fri Nov 01, 2002 9:44 am
Location: Mexico City, Mexico

Post by sixpackmx » Wed Nov 08, 2006 10:47 pm

You can use the request variables $_POST and $_GET:

For example:

$_POST["variable"]

$_GET["variable"]

spfdhost
Nothing better to do.
Posts: 111
Joined: Tue Jun 21, 2005 6:53 pm

Post by spfdhost » Thu Nov 09, 2006 6:20 am

m2 is right REQUEST_METHOD will tell you whether its POST, GET, HEAD, or PUT.

I can't tell from your question but if its for form security theunknownhost is on the right track but I would do it a bit differently, as the remote POST call could check your page source and just post that variable also.

I would create a one time random token and put it in a session variable and then put it in a hidden form field. The on submit check the form field against the session field for a match to determine if the post was good.

User avatar
Sapphyre
Official Test Penquin
Posts: 3337
Joined: Tue Nov 19, 2002 2:50 am
Location: Canada

Post by Sapphyre » Thu Nov 09, 2006 8:37 pm

Thanks for your help.

Yes, the presence of any REQUEST_METHOD value will indicate the data came from a form submission. Its absence will tell me the data came in otherwise. In fact, I discovered that all of the usual CGI ENV vars which we are accustomed to seeing are NOT SET when the data is incoming via email messages piped to the script. in this case the ENV vars which are set are those set by procmail during processing and delivery of the email message.

I will use SERVER_PORT whose value will be 80 or 443 (via https) or it will be completely unset/absent if the data is coming in via email message piped to the script.

Similarly, a third set of ENV vars are present when running a script directly via command line. In that case, what you get are ENV vars set by the shell program.

I just need different parsing subroutines depending on whether the info is submitted via form or via email. There doesn't seem to be one ENV var that is present in both cases. This needs to be determined before any data from the submission can be read, so an extra form field will not work.

Thanks again.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest