26288 total geeks with 3498 solutions
Recent challengers:
 Welcome, you are an anonymous user! [register] [login] Get a yourname@osix.net email address 

Articles

GEEK

User's box
Username:
Password:

Forgot password?
New account

Shoutbox
MaxMouse
It's Friday... That's good enough for me!
CodeX
non stop lolz here but thats soon to end thanks to uni, surely the rest of the world is going good?
stabat
how things are going guys? Here... boring...
CodeX
I must be going wrong on the password lengths then, as long as it was done on ECB
MaxMouse
lol... the key is in hex (MD5: of the string "doit" without the "'s) and is in lower case. Maybe i should have submitted this as a challenge!

Donate
Donate and help us fund new challenges
Donate!
Due Date: May 31
May Goal: $40.00
Gross: $0.00
Net Balance: $0.00
Left to go: $40.00
Contributors


News Feeds
The Register
Wikileaks leaks
documentary script
about Wikileaks
Investor Icahn
needs a loan of
$7bn to tick off
Mike Dell
World"s richest
hobo (Apple) has
worked "tax-free"
in Ireland since
"80s
Security
Twitteratti:
Twitter"s 2FA does
sweet FA for biz
Judge: Evidence
will likely show
Apple DID fix ebook
prices
ServiceKey, Oracle
end "grey market"
code spat without
bloodshed
BBC suspends CTO
after it spaffs
£100m on doomed IT
system
Open wide, Google:
Here comes an
advertising
antitrust probe
El Reg drills into
Office 365: Email
migration
Daft tweet by
Speaker Bercow"s
loquacious wife DID
libel lord
Slashdot
UC Berkeley Group
Working On Creating
Inexpensive 3-D
Printer Materials
FiOS User Finds
Limit of
"Unlimited" Data
Plan: 77 TB/Month
Xbox One Used Game
Policy Leaks:
Publishers Get a
Cut of Sale
Possible Collision
Between
Cube-satellite and
Old Space Junk
AT&T Quietly
Adds Charges To All
Contract Cell Plans
Drupalcon Attendees
Come Together To
Build Help4ok.org
In 24 Hours
Twitter"s New
Money-Making Plan:
Lead Generation
Bandages That Can
Turn Off Genes
Encourages Wound
Healing
BT Runs an 800Gbps
Channel On Old
Fiber
Australian Police
Move To Make 3D
Printed Guns
Illegal
Article viewer

Receiveing Form Data Using the Post Method



Written by:dimport
Published by:NeorageX
Published on:2003-06-21 07:19:46
Topic:Perl
Search OSI about Perl.More articles by dimport.
 viewed 12292 times send this article printer friendly

Digg this!
    Rate this article :
In this article I will show how to receive form data using the post method, and explain what the code is doing at each step.

With the POST method, form data is sent in an input stream from the server to your CGI. To get this data, store it, and decode it, we'll use the following block of code:

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
    ($name, $value) = split(/=/, $pair);
    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    $FORM{$name} = $value;
}

Let's look at each part of this.
First, we read the input stream using this line:

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

The input stream is coming in over STDIN (standard input), and we're using Perl's read function to store the data into the scalar variable $buffer.
You'll also notice the third argument to the read function, which specifies the length of data to be read.
We want to read to the end of the CONTENT_LENGTH, which is set as an environment variable by the server.

Next we split the buffer into an array of pairs:

@pairs = split(/&/, $buffer);

As with the GET method, form data pairs are separated by & signs when they are transmitted, such as fname=joe&lname=smith.

Now we'll use a foreach loop to further splits each pair on the equal signs:

foreach $pair (@pairs) {
    ($name, $value) = split(/=/, $pair);

The next line translates every "+" sign back to a space:

$value =~ tr/+/ /;

Next is a rather complicated regular expression that substitutes every %HH hex pair back to its equivalent ASCII character, using the pack() function.
The argument "C" in pack("C", hex($1)) tells the pack() function that it will be passed an unsigned char value.

$1 is a variable that matches the whatever is in the first set of parenthesis.

$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

Finally, we store the values into a hash called %FORM:

$FORM{$name} = $value;

The keys of %FORM are the form input names themselves. So, for example, if you have three text fields in the form - called name, email-address, and age - you could refer to them in your script by using $FORM{'name'}, $FORM{'email-address'}, and $FORM{'age'}.

This article was originally written by pyroinfernia

Did you like this article? There are hundreds more.

Comments:
Anonymous
2009-01-02 03:04:58
sdsads
Anonymously add a comment: (or register here)
(registration is really fast and we send you no spam)
BB Code is enabled.
Captcha Number:


Blogs: (People who have posted blogs on this subject..)
bb
List the number of files in a directory and each subdirectory on Fri 29th Sep 11am
I needed to get the number of files in a directory and recurse through all subdirectories doing the same. I nedded it so i could compare two sets of directory trees for something I was working on. My Irish chum Enstyne came up with this Perl solution w

Test Yourself: (why not try testing your skill on this subject? Clicking the link will start the test.)
Perl: The basics by sefo

Basic knowledge on often used Perl features


     
Your Ad Here
 
Copyright Open Source Institute, 2006