You are on page 1of 63
PHP MySQL Conference April 10, 2003. San Jose Rasmus Lerdorf http://lerdorf.com/mysqlconf.pdf Slide 1/51 The Good Old Days April 10, 2003 Handling simple data coming from a form took something like this to do in C: #include #include #include #include #define ishex(x) (((x) >= '0" @& (x) <= 19") || ((x) >= tal a6 Ge) <= TET) (be) >= TAN aS (x) <= TEN) int htot(char *s) { int value; char cy ce = sl0l; Lf (Lsupper (c)) © = tolower(c); value=(c >= '0' 4 ¢ <= 199 7c ~ "0! te ~ tat + 10) * 16; © = s(ll: Lf (1supper (c)) ¢ = tolower(c); value $= >= '0" && c <= 19" 2 e- tO" te tat + 10; return (value); ) void main(int argc, char *argv[]) { char params, data, dest, s, *tmp; char name, age; puts "Content-type: text/htm1\r\n"); puts ("Form Example") ; puts ("My Example Form"); puts ("
") ; puts("Name: ") ; puts("Age: "); puts("
"); puts ("
"); data = getenv ("QUERY_STRING"); if(data 4& *data) { params = data; dest while(*data) { if(data=='+") dest="; else if (data == '%' 4& ishex((data+1))4éishex(*(data+2)}) { tdest = (char) htoi(data + 1); data: } else dest = data; datat+; dest++; data; ) tdest_ = '\0"; 5 = strtok (params, "6"); do ( tmp = strchr(s," af(emp) { Emp = 1\0"; if (Istrenp(S,"name")) name = tmptl; else f(istrémp(s,"age")) age = emptl; dM } } while (s=strtok (NULL, "6")); print£("Hi s, you are s years old\n",name, age); ) puts ("") ; Slide 2/51 April 10, 2003 The Perl alternative Perl became an obvious choice because it was made for text processing. The same thing in Perl using CGLpm: use CGI qui: print header, print start_html (‘Form Example"), ‘AL('My Example Form'), start_form, 7", textfield('name'), andard) ; , textfield (tage'), end_form; if(param()) | print "ii ",em(param('name')), You are ",em(param("age'}), "years old"; ) print end_ptml; Much easier both to read and to write, at least to people with a bit of a programming background.

You might also like