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:
variables in URL...?

 



69_vette
Deleted

Mar 3, 2000, 10:08 AM

Post #1 of 7 (1285 views)
variables in URL...? Can't Post

How can I read variables in the URL pointing to the script?

i.e. - whatever/cgi-bin/data.pl?id=footer

The idea, anyway, is to avoid making seperate scripts when the only difference in those scripts is that ONE variable...

Anything that achieves that same result would be great...

Thanks!


69_vette
Deleted

Mar 3, 2000, 12:00 AM

Post #2 of 7 (1285 views)
Re: variables in URL...? [In reply to] Can't Post

Thanks!

That helps...

Is there a way to do it without CGI.pm though?


darian
Deleted

Mar 3, 2000, 12:57 AM

Post #3 of 7 (1285 views)
Re: variables in URL...? [In reply to] Can't Post

There sure is. Just add the code below to your script and use this format for your variables:

$in{'variable'}

make sure you replace the word variable with the actual name passed to the program.

sub parse {
local (*in) = @_ if @_;
local ($i, $key, $val);
if ($ENV{'REQUEST_METHOD'} eq "GET")
{
$in = $ENV{'QUERY_STRING'};
}
elsif ($ENV{'REQUEST_METHOD'} eq "POST")
{
read(STDIN,$in,$ENV{'CONTENT_LENGTH'});
}
@in = split(/&/,$in);
foreach $i (0..$#in)
{
$in[$i] =~s/\+/ /g;
($key, $val) = split(/=/,$in[$i],2);
$key =~s/%(..)/pack("c",hex($1))/ge;
$val =~s/%(..)/pack("c",hex($1))/ge;
$in{$key} .= "\0" if (defined($in{$key}));
$in{$key} .= $val;
}
}


perlkid
stranger

Mar 3, 2000, 12:59 AM

Post #4 of 7 (1285 views)
Re: variables in URL...? [In reply to] Can't Post

 
Yes,

You can put this sub at the bottom of the program

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


sub parse_form {
my (@pairs, %in);
my ($buffer, $pair, $name, $value);

if ($ENV{'REQUEST_METHOD'} eq 'GET') {
@pairs = split(/&/, $ENV{'QUERY_STRING'});
}
elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
}
else {
&cgierr('You cant run this script from telnet/shell.');
}

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

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

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

($value eq "---") and next PAIR;
exists $in{$name} ? ($in{$name} .= "~~$value") : ($in{$name} = $value);
}
return %in;
}
</pre><HR></BLOCKQUOTE>

and at the top with all of your variables you can put

%in = &parse_form;

then you could use it like so,

if($in{'id'} eq "footer") {&footer_html;}

perlkid


perlkid
stranger

Mar 3, 2000, 1:00 AM

Post #5 of 7 (1285 views)
Re: variables in URL...? [In reply to] Can't Post

 
Ha Ha,

Ok, never mind, I guess darian and I both had the same thing in mind.

perlkid


darian
Deleted

Mar 3, 2000, 10:28 AM

Post #6 of 7 (1285 views)
Re: variables in URL...? [In reply to] Can't Post

Not sure I really understand what you are looking for but I will give it a try. I would simply read in all the data and allow CGI.pm to parse them. Then just use what you want out of them. You don't have to use all of what is passed in the url.

use CGI qw(:standard);
$q = new CGI;
$first = $q->param('first');
$second = $q->param('second');

if ($second) { &second_sub; }

With this little script we do nothing with the $first as we don't need it at this point.


Cure
User

Mar 3, 2000, 12:25 PM

Post #7 of 7 (1285 views)
Re: variables in URL...? [In reply to] Can't Post

Hi

Example::-> If a user puts the word admin in the url it will call the subroutine admin in the script..

#!/usr/bin/perl
use CGI;
$q = new CGI;
print $q->header;

if("admin" eq $ENV{'QUERY_STRING'})
{&admin}
else {exit}
sub admin {

print $q->start_html(-title=>"admin",
-author=>'me',
-BGCOLOR=>'white');

print qq~
<CENTER><H4>IM THE ADMIN</h4></center>
~;
print $q->end_html;
}


Cure

[This message has been edited by Cure (edited 03-03-2000).]

 
 


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

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