CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Intermediate:
Need to Encode data for POST or GET



Feb 5, 2000, 6:14 PM

Post #1 of 5 (6025 views)
Need to Encode data for POST or GET Can't Post

I am creating a hyperlink to a Perl script. The hyperlink will pass a user's name and other information. I have two problems.
1) Since the GET does not allow spaces and other special characters to be passed, I've tried "encoding the data". Unfortunately, the "encoded" information is not correctly interpreted. I'm using
$string =~ s/(\W)/sprintf(%%%x, ord($string)/eg

When the data is encoded ALL special characters are assigned an arbitrary value (i.e. space may be %42 one time and %54 another)

When received by the hyperlinked program, the symbol is usually the first letter of the second word.

There's got to be an easier way to do this!

2)I'd like to pass the data using POST rather than GET. However, I only know how to attach to a URL using /cgi-bin/

Any help will be DEEPLY appreciated!


Feb 5, 2000, 6:59 PM

Post #2 of 5 (6025 views)
Re: Need to Encode data for POST or GET [In reply to] Can't Post

Rita, if you require the CGI module in your program, there's 2 subroutines in it that'll help you url encode/decode (escape and unescape).

If you'd rather not load into your program, then you could just use the following 2 subroutines, taken from

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

# unescape URL-encoded data
sub unescape {
shift if ref($_[0]);
my $todecode = shift;
return undef unless defined($todecode);
$todecode =~ tr/+/ /; # pluses become spaces
$todecode =~ s/%([0-9a-fA-F]{2})/pack("c",hex($1))/ge;
return $todecode;

# URL-encode data
sub escape {
shift if ref($_[0]) | | $_[0] eq $DefaultClass;
my $toencode = shift;
return undef unless defined($toencode);
$toencode=~s/([^a-zA-Z0-9_.-])/uc sprintf("%%%02x",ord($1))/eg;
return $toencode;

2) I'm confused. Are you referring to just passing data, or being able to, let's say, bookmark a page after data has been passed through POST. The latter won't work. The former can have data passed via html's <INPUT TYPE=HIDDEN VALUE="whatever"> tag. If I didn't answer your question, could you please elaborate a little?

Good luck!

[This message has been edited by Jasmine (edited 02-05-2000).]


Feb 5, 2000, 8:18 PM

Post #3 of 5 (6025 views)
Re: Need to Encode data for POST or GET [In reply to] Can't Post

Thanks for the information. Are the names of the modules in "escape" and "unescape"?

Problem #2 comes about when I create a webpage (NOT a form) from a database. The webpage contains a hyperlink to another perl program and looks like this

print "<A HREF=/cgi-bin/>Next Page</A>", "\n";

This calls and passes the two values to the Perl script. However, the amount of data that can be passed is server dependent. I was wondering if there is a way to invoke POST rather than GET from the hyperlink.

Thanks again.


Feb 5, 2000, 9:19 PM

Post #4 of 5 (6025 views)
Re: Need to Encode data for POST or GET [In reply to] Can't Post

The "escape" and "unescape" in aren't modules, they're subroutines within the module.

If you're just trying to pass 2 values to your program, there shouldn't be a length problem.

Unless you use a Javascript thingie to post to a form using a text link (not advised), or an image form submit button, there's no other way I can think of right now to post without the ugly grey buttons.

Hope this helps,


Feb 5, 2000, 9:48 PM

Post #5 of 5 (6025 views)
Re: Need to Encode data for POST or GET [In reply to] Can't Post

Hi Rita:

When you use hyperlinks to carry information
it uses GET. To use POST(so people cant see the information on the url) you have to use hidden tags not hyperlinks.

So, for hyperlinks use the escape subroutine to filter the url coding(this way you can have spaces and etc in your password(or whatever) and the escape subroutine will encode it to the url specifications.

In this example, I have a form field name name1(user-name) and name2(user-password)

<A href="<A HREF=/cgi-bin/{[$q->escape($q->param('name1'))]}&name2=@{[$q->escape($q->param('name2'))]}"

$q->param('name1') = this gets the value of the name1(user-name)

$q->param('name2') = this gets the value of the name2(user-password)

The escape subroutine will allows the user to have spaces and etc for there user-name and there password because the escape subroutine will filter it for them.

To do this make sure you have this at the top of your script

use CGI;
$q = new CGI;



Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives