Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

url from online form written to file gives percent F not forward slash 1

Status
Not open for further replies.

latigerlilly

Programmer
Sep 11, 2005
6
US
Hi,

I have a cgi program written in PERL that takes a URL from an online form and then it writes the URL to a file. However, the colon and two forward slashes get turned into percent 3A percent 2F percent 2F. How do I fix it so I just get a normal URL? Thank you for your help. Below is the actual URL output as it is written in the file.

http%3A%2F%2F
Below is the actual script.

#!/usr/bin/perl

#COMMENT - PARSES DATA FROM THE HTML FORM

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 {
print "Content-type: text/html\n\n";
print "<P>Use Post or Get";
}
foreach $pair (@pairs) {
($key, $value) = split (/=/, $pair);
$key =~ tr/+/ /;
$key =~ 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 =~s/<!--(.|\n)*-->//g;

if ($formdata{$key}) {
$formdata{$key} .= ", $value";
} else {
$formdata{$key} = $value;
}
}

### assigns values to variables. These variables are from the online form.

$name = $formdata {'name'};
$password = $formdata {'password'};
$pic = $formdata {'pic'};
$link = $formdata {'link'};
$description = $formdata {'description'};

### writes file

open (LOG, ">>/home/mydomain/flock (LOG,2);

print (LOG "$name|$password|$pic|$link|$description\n");

flock (LOG,8);
close (LOG);

print "Content-type: text/html\n\n";
print "Would you like to input another auction? <A HREF=\"
### error message

sub ErrorMessage {
print "Content-type: text/html\n\n";
print "The server can't open the file. It either doesn't exist or the permissions are wrong. \n";
exit;
}

### note: I've changed the actual domain name of my script to mydomain.com so that my server won't
### get hacked into. The program isn't finished writing yet and website safety features aren't
### installed yet. Again, thanks for your help.
 
Look into using CGI.pm, this should escape your variables automatically, and make your life an awful lot easier ;-)
HTH
--Paul


Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Code:
[b]#!/usr/bin/perl[/b]

$url = 'http%3A%2F%2F[URL unfurl="true"]www.ebay.com';[/URL]

$url =~ s|%3A%2F%2F|://|;

print $url;


Kind Regards
Duncan
 
PaulTEG is doubtless correct with his advice - however i have never used CGI.pm. I am probably a muppet for not doing so... I guess i enjoy trying to understand the 'nuts & bolts'


Kind Regards
Duncan
 
!Muppet, but luddite, you won't believe the amount of st*ff CGI does for you, and it's a standard module

You don't live near Todpuddle do you ;-)

--Paul

Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Thanks duncdude!

You set me on the right track!!! I thought that it was a hexadecimal conversion to ascii thing, doh!!!

The URLs used sometimes has many forward slashes in it, so that snippet of code didn't work, BUT you set me on the right track (thanks!!!) so I was able to do the following to make it work:

#!/usr/bin/perl

$url = 'http%3A%2F%2F
$url =~ s|%3A|:|;
$url =~ s|%2F|/|g;

print $url;

You're the best, man!!! Especially because I havee no idea what perl.pm is. I'm guessing that it's some module of perl that I'm not using which contains lots of functions you can call?

Much appreciation!
 
no problem dude! thanks for your kind comments


Kind Regards
Duncan
 
justone thing - i used pipes for the s||| rather than forward-slashes, for readability:-

$url =~ s/%2F/\//g;

... is not as pretty - and you would have to escape the forward-slash in the replacement section

Sorry if you already knew this!


Kind Regards
Duncan
 
In my Perl book, it says to use the forward slash, but when I tried it with pipes, it still worked. Whatever. As long as it works....
 
just know it works ...


Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
what happens if someone enters a pipe in the data sent to the script and then it gets printed to the file? What appens to the file if there are newline characters in the data sent from the form? What happens if some shmuck decides to upload a 20 gigabyte file to your server using that script to recieve the file upload? Looks like the script will also process multiple input fields of the same name and append them together with a comma, is that what you want?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top