Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm setting the max post size to my CGI script in kilobytes, but I want to know how many chacters it is. I'd like to limit it to 50 characters.

Max post size is set by:
"$CGI::POST_MAX=1024 * 100; # max 100K posts
How many characters would this be?

Replies are listed 'Best First'.
Re: CGI Post Data Size
by moritz (Cardinal) on Sep 12, 2007 at 05:32 UTC
    It would be 102400 Bytes. If you want to set it to 50 bytes, set it to 50.

    Note that the mapping between bytes and characters is depending on the encoding you use. If you use latin1 for example, each character is one byte.

    But if you want to set such a small limit, chances are that what you really want is to limit the input size of your form elements.

      But remember to check field size again on the server side with Perl regardless of setting field limit in HTML form.

Re: CGI Post Data Size
by awohld (Hermit) on Sep 12, 2007 at 06:28 UTC
    Setting $CGI::POST_MAX to 102400 is approx 102,400 characters minus some overhead. This is more of a DoS Attack counter measure.

    Limiting in the HTML form is the least secure way to limit data. It is easy to subvert using something like LWP.

    You can go a little further and limit using substr in your script:
    #!/usr/bin/perl -w use strict; my $long_string = "Long String"; print $long_string . "\n"; # limit string to 3 characters in length $long_string = substr($long_string, 0, 3); print $long_string . "\n";