#!/usr/bin/perl -- # NOTE: You must change the line above to point to the path to Perl # on your system. ######################################################################### ##### SFEBMIcalc v1.0 # ##### Copyright 2004, Kristina L. Pfaff-Harris, www.tesol.net/scripts # # # # A simple Body Mass Index calculator. Enter your height and weight, # # and the program will return your BMI. # # # # Please read the README file for instructions on how to set this up, # # what to change, and how to use this program. # # # ######################################################################### ##### Licensing: # ##### # ##### This program may be used free of charge under the following # ##### conditions: # ##### # ##### 1. All instructions and Copyright lines must remain unchanged. # ##### 2. All pages generated by the program must contain one of the # ##### following pieces of HTML code: # ##### # ##### SFEBMIcalc v1.0 is # ##### Copyright 2004, Kristina Pfaff-Harris and can be found # ##### at: # ##### # ##### http://www.tesol.net/scripts # ##### OR: # ##### # ##### # ##### # ##### 3. You may not sell or distribute this program. You may charge # ##### a reasonable fee for installing it for a client as long as # ##### you make it clear that you are not the author, and you are # ##### not selling the program to them: only charging for installing # ##### it. # ##### 4. You agree that this program is offered without warranty of # ##### any kind, including warranty of fitness for a particular # ##### purpose. You further agree that the author and all sites # ##### associated in any way with this program are not liable for # ##### any damage or loss incurred as a result of using this program.# ######################################################################### ##### # ##### IMPORTANT INSTRUCTIONS: # ##### # ##### In this program, I have put **CHANGE** in all the places where # ##### you will need to modify the program to run on your server, so # ##### that you can easily find all the places where changes are # ##### necessary. This program must be chmod 755 or 775 in order to # ##### work, and the html page must be chmod 644. # ##### # ######################################################################### $| = 1; %data = &parse_data(); print "Content-type: text/html\n\n"; # Note: On most servers, this is fine the way it is. However, on some # servers, you may have to change this to the full URL of the # sfebmicalc.cgi program. For example: # $cgi_url = "http://www.your_site.com/cgi-bin/sfebmicalc.cgi"; # If everything seems to work, then leave this as it is. $cgi_url = "$ENV{'SCRIPT_NAME'}"; ######################################################################### ##### # ##### This is the beginning of the part where you must **CHANGE** # ##### things. # ##### # ######################################################################### # First, let's choose a color scheme. You'll need to know "hex" # colors for fonts and bacgrounds for this to work effectively. # If you don't know the hex colors, try plain words such as "blue", # "red" or "black" or leave these as they are. The hex colors for # the way the demo program is are below. You may **CHANGE** these # to colors more in keeping with your site, or just use a CSS # stylesheet in $header below. $text_color = "000000"; $page_background_color = "FFFFF0"; $page_links_color = "0000FF"; $page_active_links_color = "FF0000"; $page_visited_links_color = "0000FF"; $table_background_color= "F4E3C8"; $table_cell_color= "FFFFFF"; # $header is the HTML header that will be generated by the results page # of the program. You may **CHANGE** this to any HTML code you like. # The caveats here are as follows: First, any of the following # characters: # " % @ $ \ # must have a backslash (\) in front of them like this: # \" \% \@ \$ \\ # or the program will not work. ONLY change between the lines that # say and # You may also leave this as it is. $header = qq[ SFEBMIcalc v1.0
SFEBMIcalc v1.0: Calculate your Body Mass Index

]; # $footer is the HTML code that will go at the bottom of pages # generated by the program. The same rules apply as to $header # above. You may also leave this as it is, and it will work fine. # Again, ONLY change between the lines that say # and $footer = qq[
]; # Now, would you like the default to be Kilograms, or Pounds for the # person entering his or her weight? If you would like it to be # Kilograms, please set this to $default_weight = "k"; Otherwise, # set this to $default_weight = "p"; $default_weight = "p"; # And, would you like the default to be Centimeters, or Inches for the # person entering his or her height? If you would like it to be # Centimeters, please set this to $default_height = "c"; Otherwise, # set this to $default_height = "i"; $default_height = "i"; # While we're on the subject, if you spell "Centimeters" differently # than I do, please feel free to change the spelling of "Centimeters" # here. :-) $centimeters_spelling = "Centimeters"; # Okay, one last thing: Body Mass Index is really a somewhat flawed # measurement when it comes right down to it. What's really unhealthy # is body fat percentage, rather than a weight-height thing. $notes # below is used by the "About BMI" popup window for this script. # I have put in an explanation for this here. You may **CHANGE** it # if you like to an explanation you prefer. Only change in between the # and tags. $notes = qq[
About Body Mass Index


It's important to realize that BMI is not the most accurate determination of whether or not you are at an unhealthy weight. For example, if you are extremely muscular, your BMI may show as "Overweight" or even "Obese," when you're not. By the same token, if you are a "Normal" BMI, but have a very high body fat percentage, you may have the same health risks as someone with an "Obese" BMI.

BMI is only one tool in your health arsenal. Body fat percentage is a much better predictor of health risks than the ratio of your height to your weight.

--Close Window-- ]; # $about_bmi_height is for the height (in pixels) of the popup explanation # window. $about_bmi_height = "400"; # $about_bmi_width is for the width (in pixels) of the popup explanation # window. $about_bmi_width = "300"; ######################################################################### ##### # ##### This is the end of the part where you must **CHANGE** # ##### things. Feel free to look through the code if you are # ##### interested (no pun intended) in such things, but don't change # ##### anything beyond this point unless you know what you're doing. # ##### # ######################################################################### # First, we get the data submitted on the form into variables we can # use. # If you remove this link, without replacing it with a similar # HTML comment (as above in the License section) you are in violation # of the license for this program. No, I don't have the resources to # enforce this, and I know that some people will remove it anyway. # But honestly, since you didn't have to pay for this, is the link # really too much to ask? :) $footer = "
Powered by SFEBMIcalc v1.0
$footer"; ################################################################# if($data{'FA'} eq "AboutBMI"){ &print_explanation(); } print $header; print &get_form; $kilos_or_pounds = $data{'wt'}; $centimeters_or_inches = $data{'ht'}; $weight = $data{'weight'}; if($weight * 1 ne $weight && $weight ne ""){ print "Sorry, incorrect weight. Numbers only.
"; } $height = $data{'height'}; if($height * 1 ne $height && $height ne ""){ print "sorry, incorrect height. Numbers only.
"; } if($kilos_or_pounds eq "k"){ $calc_weight = &convert_to_pounds($weight); } else { $calc_weight = $weight; } if($centimeters_or_inches eq "c"){ $calc_height = &convert_to_inches($height); } else { $calc_height = $height; } # Now everything is in pounds and inches, since BMI is relative anyway. if($calc_height * $calc_height != 0){ $BMI = sprintf("%.02f", ( $calc_weight / ($calc_height * $calc_height)) * 703); } $about_bmi_link = qq[About BMI]; print qq[
]; print "Your BMI is $BMI." if $BMI; print "Your BMI is unknown: calculate it!" if !$BMI; print qq[ ]; print $about_bmi_link; print qq[
]; print $footer; exit(); sub get_form { $form = "
What's your Body Mass Index?

Your weight:
Your Height:
 
BMI Meaning
Over 29.9 Obese
25.0 - 29.9 Overweight
18.5 - 24.9 Healthy
Under 18.5 Underweight
"; $form; } sub parse_data { local($string); # get data if ($ENV{'REQUEST_METHOD'} eq 'GET') { $_ = $string = $ENV{'QUERY_STRING'}; tr/\"~;/_/; $string = $_; } else { read(STDIN, $string, $ENV{'CONTENT_LENGTH'}); $_ = $string; $OK_CHARS='a-zA-Z0-9=&%\n\/_\-\.@'; tr/\"~;/_/; $string = $_; } # split data into name=value pairs @data = split(/&/, $string); # split into name=value pairs in associative array foreach (@data) { split(/=/, $_); $_[0] =~ s/\+/ /g; # plus to space $_[0] =~ s/%(..)/pack("c", hex($1))/ge; # hex to alphanumeric $data{"$_[0]"} = $_[1]; } # translate special characters foreach (keys %data) { $data{"$_"} =~ s/\+/ /g; # plus to space $data{"$_"} =~ s/%(..)/pack("c", hex($1))/ge; # hex to alphanumeric } %data; # return associative array of name=value } sub convert_to_pounds { my($kilos) = $_[0]; my($pounds) = $kilos * 2.20462262; $pounds; } sub convert_to_inches { my($centimeters) = $_[0]; my($inches) = $centimeters * 0.393700787; $inches; } sub print_explanation { print "$notes"; exit(); }