Click to See Complete Forum and Search --> : writing a file with a URL variable


magnus
01-10-2003, 04:19 PM
Hi all
I real green when it comes to cgi and perl so please excuse the ignorance

I am trying to write a cgi script that will take a variable in the form of a URL string (like http://10.0.0.1/cgi-bin/myname.cgi?name=joe) the cgi script(myname.cgi) will take the variable name and write a file on my web server with the value of the variable passed and also put that name in the file along with some other static entries

So the file would be named joe
and the contents of the file would be
Joe (variable from the url string)
Company = abc company (this is static and will be added for all )
Tel = 5551212 (this is static and will be added for all )

Does some one have an example of this that I could look at to get a better idea on how to write it

Thanks
Magnus

pyro
01-10-2003, 05:17 PM
I don't know what you all know, but maybe this will get you going.

Use $QUERY_STRING to parse the url and get the info after the ? Do something like $variable = $QUERY_STRING; Then just open your file and write the variable and the static data. I don't know the exact syntax in Perl off the top of my head (I use PHP) so I can't give you exact code, sorry.

magnus
01-10-2003, 05:23 PM
Thanks for the quick reply

That is a definete start for me.

Would anyone know some of the syntax for perl on how to do that

Thanks
Magnus

pyro
01-10-2003, 05:38 PM
Ok, I hope I did all this right. If it doesn't work, let me know. Also, make sure you set the permissions on file.txt to 777, upload this file in ASCII and set the permissions to 755 (I think).

Warning: This is untested...

#!/usr/bin/perl

$variable = $QUERY_STRING; #parse url to get everything after the ?

open(MESSAGE, ">>file.txt"); #open file.txt and append data to it
flock(MESSAGE, 2); # lock the file to prevent file corruption
print MESSAGE "$variable"; # pring the query string.
print MESSAGE "Company";
print MESSAGE "Telephone";
flock(MESSAGE, 8); #release the file
close (MESSAGE); #close the file

magnus
01-10-2003, 06:02 PM
Thanks I will give that a shot

Magnus

pyro
01-10-2003, 06:50 PM
The above does not work. I'm going to be gone for a bit, but will look at it soon.

Here is one error that I noticed

$variable = $QUERY_STRING;
should be:
$variable = $ENV {'$QUERY_STRING};

There may be more...I'll get back to you.

jeffmott
01-10-2003, 10:49 PM
#!/usr/bin/perl -wT

use strict;
use CGI 'param';
use Fcntl ':flock';

# retrieve value of the 'name' parameter
my $fn = param('name');

if (!defined $fn) {
  die 'File name not specified';
}

# make sure $fn does not have a melicious value
# only allow alphanumeric and underscore
if ($fn =~ m/\W/) {
  die 'Invalid file name';
}

# $fn is safe, untaint it
# see Perl security (http://www.perldoc.com/perl5.8.0/pod/perlsec.html) about taint checks
$fn =~ m/^(.*)$/s;
$fn = $1;

open MSG, ">$fn" or die $!;
flock MSG, LOCK_EX or die $!;
print MSG "$fn\nCompany = abc company\nTel = 5551212" or die $!;
close MSG or warn $!;

pyro
01-10-2003, 11:46 PM
Ok, since Jeff has posted, I'd recommend using his, as he is definitly more knowledgeable than me in Perl. Like I said before, I use PHP, and while they are similar, my Perl scripts often don't work... :D

pyro
01-10-2003, 11:52 PM
Just made that change noted above to my script and it now works fine. :)

#!/usr/bin/perl

$variable = "$ENV{'QUERY_STRING'}"; #parse url to get everything after the ?

print "Content-type: text/html\n\n";

open(MESSAGE, ">>file.txt"); #open file.txt and append data to it
flock(MESSAGE, 2); # lock the file to prevent file corruption
print MESSAGE "$variable\n"; # pring the query string.
print MESSAGE "Company\n";
print MESSAGE "Telephone\n\n";
flock(MESSAGE, 8); #release the file
close (MESSAGE);

magnus
01-12-2003, 03:37 PM
Thank you to all

I will give that one a shot and see if it works for me

Magnus