Please help me with how to use client inputs in a form as part of arguments in a CGI script following a button return. I want a client to specify a name which i can then use in perl script to create a folder for that client.

Dani AI

Generated

A short, practical add-on to the answers from , and :

CGI.pm is a fine, simple tool for reading form fields (as suggested above), but note it was removed from Perl core in Perl 5.22 — it may need to be installed from CPAN on modern systems. For new or growing projects consider PSGI/Plack or a lightweight framework instead of raw CGI. (Perl 5.22 changes) (Plack/PSGI overview). (perldoc.perl.org)

Checklist of correctness and security (apply before creating folders):

  • Run the script with taint checks (-T) for server-exposed programs and validate every external value. (Perl security / taint mode).
  • Strip whitespace and directory components, reject slashes/backslashes and suspicious characters, and enforce a strict allowlist (max length, allowed chars). Use File::Spec/File::Basename helpers rather than hand-concatenating paths. (File::Spec functions).
  • Create directories with File::Path::make_path inside an eval to avoid race conditions; do not assume mkdir will always succeed. Remember created mode is affected by umask. (File::Path::make_path docs). (perldoc.perl.org)

Safe minimal pattern (adapt and harden to taste):

#!/usr/bin/perl -wT
use strict;
use warnings;
use CGI;
use File::Spec::Functions qw(catdir);
use File::Basename qw(basename);
use File::Path qw(make_path);

my $q = CGI->new;
print $q->header('text/plain');

my $raw = $q->param('name') || '';
$raw =~ s/^\s+|\s+$//g;
my $name = basename($raw);            # drop any path components
$name =~ s/[^\w\.-]//g;               # allow A-Z a-z 0-9 _ . -
$name = substr($name,0,32);           # limit length

die "invalid name\n" unless $name =~ /^[A-Za-z0-9]/;

my $base = '/path/to/parent';
my $dir  = catdir($base, $name);

eval { make_path($dir, { mode => 0755 }) };
if ($@) {
  print "error creating directory: $@\n";
} else {
  print "created $dir\n";
}

Common causes of failure: webserver user lacks write permission on the parent directory, SELinux policies, wrong shebang or missing modules (CGI may not be installed), or umask producing unexpected permissions. Check ownership/permissions and server logs; use safer defaults (0755) rather than 0777. See mkdir/mode and module docs for details. (mkdir docs) (CGI on CPAN). (perldoc.perl.org)

For : prefer a sanitized slug or a generated id stored in a database rather than raw client-supplied folder names; it avoids collisions and most security pitfalls.

Recommended Answers

All 3 Replies

Try using the CGI module. It makes doing CGI development in Perl extremely easy (at least the CGI bits).

Try this at a command line:

perldoc CGI

A simple example... (note: more validation should be done, this is just an example...)

// a basic html form

<form action='script.pl' method='post'>
NAME: <input type='text' name='name' size'8' maxlength='8' />
<br />
<input type='submit' name='button' value='Folder Name' />
</form>

// the perl script (script.pl)

#! /usr/lib/perl

use CGI;
use strict;

my $obj = new CGI;

my $name = $obj->param ( 'name' );

my $base = './docs/folders/';

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

if ( $name eq '' )
{
	print "the directory name was not entered!\n";
}
elsif ( $name !~ m/^[a-z0-9]{1,8}$/i )
{
	print "the directory name was not valid!\n";	
}
else
{
	if ( -e $base . $name )
	{
		print "can not create directory, directory already exists!\n";
	}
	elsif ( ! mkdir ( $base . $name, 0777 ) )
	{
		print "can not create directory\n";
	}
	else
	{
		print "new directory created\n";
	}
}

exit(0);

me!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.