Gossamer Forum
Home : General : Perl Programming :

Script what shows an image and records data?

Quote Reply
Script what shows an image and records data?
Hi,

in a script i'd like to show an image simply like:

print "Location: http://www.domain.com/images/clan.gif\n\n";

and additionally i'd like to catch several parameters from the url like "image.cgi?user=name&image=grafic" and storing those into a txt-file before showing the image ... could anyone post a simple sample for me?

Thanks a lot.


Quote Reply
Re: Script what shows an image and records data? In reply to
Do you mean like:

#!/usr/bin/perl

use CGI qw(:standard);

&main();

sub main {

my $IN = new CGI;

my $username = $IN->param('username');
my $image = $IN->param('image');
my $url = "http://www.domain.com";
my $file = "/path/to/datafile.txt";

if (($username =~ /^\w+$/) && ($image =~ /\.(gif|jpg)$/)) {

open(FILE,">>$file") || die &error("Couldn't open file : $!");
print FILE "$username|$image";
close FILE;

print "Location: $url/$image\n\n":

} else {

&error("Invalid query string.");

}

}


sub error {

my ($msg) = shift;

print header;

print $msg;

exit();

}



I'm not sure if that is what you mean so correct me if I missed the point.
You can easily change it to work with a form if that is how the data is being passed to the script.

You would use it like:

http://www.domain.com/cgi-bin/script.cgi?username=bob&image=grafic.gif



Paul
Installations:http://wiredon.net/gt/
Support: http://wiredon.net/forum/

Quote Reply
Re: Script what shows an image and records data? In reply to
Paul,

great help - thanks for your code snippet!

A last question:

if (($username =~ /^\w+$/) && ($image =~ /\.(gif|jpg)$/)) {

How would you check an url in this line instead of an username?



Quote Reply
Re: Script what shows an image and records data? In reply to
Paul,

sorry to bother you - a last suggestion/question:

If any entry already exists in the data file - how would you prevent more than one entry per indivdual call (params)?

Thanks again!

Quote Reply
Re: Script what shows an image and records data? In reply to
To check a URL:

unless ($url =~ /^http:\/\//) {
&error("Invalid URL");
}

That will just check that it starts with http:// - what did you want to check for exactly?

To stop duplicates....change:

open(FILE,">>$file") || die &error("Couldn't open file : $!");
print FILE "$username|$image";
close FILE;


to...

open(FILE,">>$file") || die &error("Couldn't open file : $!");
@data = <FILE>;
close FILE;

foreach (@data) {
chomp $_;
($user,$img) = split(/\|/, $_);
if (($username eq $user) or ($image eq $img)) {
&error("Duplicate entry.");
}
else {
open(FILE,">>$file") || die &error("Couldn't open file : $!");
print FILE "$username|$image";
close FILE;
}
}





Paul
Installations:http://wiredon.net/gt/
Support: http://wiredon.net/forum/