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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Browser Info

 



Pro_4
User

May 17, 2001, 7:33 AM

Post #1 of 4 (818 views)
Browser Info Can't Post

hey,
I was wondering how can you take information that is stored into the browser url and get your cgi program to take it out. ( e.g. http://forums.perlguru.com/newpost.pl?Cat=&Board=Forum2). Thanks i really appreciate any information about or around this topic.

:)

People Engrossed by a Reliable Language


wickedxter
User

May 17, 2001, 11:41 AM

Post #2 of 4 (814 views)
Re: Browser Info [In reply to] Can't Post

well do this..

http://www.blah.com/cgi-bin/urltest.cgi?Url=http://www.annon.com&tryies=4

(split things by the &)
($value1, $value2) = split(/\&/,$ENV{'QUERY_STRING'});

(Now split the name & values with the =)
($Url, $Url_value) = split(/\=/,$value1);
($Tires, $Tries_value) = split(/\=/,$value2);

$Url_value will eq http://www.annon.com
$Tires_value will eq 4

Heres an example:

if(($query =~ /Url=$url/) && ($query =~ /Tries=$tries)){
($value1, $value2) = split(/\&/,$ENV{'QUERY_STRING'});
($Url, $Url_value) = split(/\=/,$value1);
($Tires, $Tries_value) = split(/\=/,$value2);

&test("$Url_value","$Tries_value");
}

sub test
{
print "Content-type: text/html\n\n";
$url = $_[0];
$tries = $_[1];

print "$url\n";
print "$tries\n";

}


Hope this helps ya...



randor
User

May 17, 2001, 11:42 AM

Post #3 of 4 (814 views)
Re: Browser Info [In reply to] Can't Post

it is called "Parseing form data"

this is a popular one that alot of people use, it places each name into a form.. example:

$form{'board'} equals Forum2
or
$form{'Cat'} equals nothing

here is the function:

sub get_form_data {
$buffer = "";
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs=split(/&/,$buffer);
foreach $pair (@pairs)
{
@a = split(/=/,$pair);
$name=$a[0];
$value=$a[1];
$value =~ s/\+/ /g;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ s/~!/ ~!/g;
$value =~ s/[\n\r]/ /sg; #remove \n
$value =~ s/\[\]//g; #remove []
push (@data,$name);
push (@data, $value);
}
%form=@data;
%form;
}

hope this helps..



rGeoffrey
User / Moderator

May 17, 2001, 2:48 PM

Post #4 of 4 (809 views)
Re: Browser Info [In reply to] Can't Post

But of course that will not help for this example.

The script is called as a GET with the information in the URL behind a question mark


Code
http://forums.perlguru.com/newpost.pl?Cat=&Board=Forum2

So the key/value pairs are hiding in $ENV{'QUERY_STRING'}, not in STDIN, which is where they would be in a POST. The earlier reply looked in the right place, but was hard coded for exactly two pairs rather than using the more standard @pairs.

And what if multiple checkboxes are picked like ?months=jan&months=feb that have the same name in the form? Only the last one will survive.

And an efficiency nit to pick. Why do you fill the @data array just to feed it to %form? You could replace the two push lines with


Code
%form{$name} = $value;

Parsing CGI forms is harder than it looks. Which is what prompted Ovid to write use CGI or die; at Perlmonks, as metioned in an earlier thread.

Or if you don't want to use the full featured CGI.pm or CGI::Lite, CGI::Minimal, or the old perl 4 cgilib.pl, you could use my favorite, CGI::Thin.

--
Sun Sep 9, 2001 - 1:46:40 GMT, a very special second in the epoch. How will you celebrate?

 
 


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

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