Saturday, December 27, 2008

Random Integer

Our PHP programmer friends have a function called randInt() which returns a random integer between two specified integers. It's time to level the playing field.


ASP

  1. function randInt(min, max)
  2.     Randomize
  3.     randInt = Int((max - min + 1) * Rnd + min)
  4. end function

View ASP implementation on Snipplr

Saturday, December 20, 2008

strip_tags()

This week we're recreating a PHP function that is extremely important for sanitizing user input. All HTML/ASP/PHP tags are stripped outright; there is no support for a whitelist of allowed tags. A whitelist can be very dangerous without much more rigorous testing to check for script-related exploits. A safer solution would be to force the user to use something like UBB code or Markdown and convert to HTML on the backend.


ASP

  1. function strip_tags(unsafeString)
  2.     dim regEx
  3.     set regEx = new RegExp
  4.     with regEx
  5.         .Global = true
  6.         .IgnoreCase = true
  7.         .Pattern = "(\<(/?[^\>]+)\>)"
  8.     end with
  9.     strip_tags = regEx.Replace(unsafeString, "")
  10.     set regEx = nothing
  11. end function

View ASP implementation on Snipplr

Saturday, December 13, 2008

isPostBack()

Today's function comes not from PHP, but ASP.NET. We're going to replicate the isPostBack() function from ASP.NET's Page object.


ASP

  1. function isPostBack()
  2.     if uCase(Request.ServerVariables("REQUEST_METHOD")) = "POST" then
  3.         isPostBack = true
  4.     else
  5.         isPostBack = false
  6.     end if
  7. end function

PHP

  1. function isPostBack()
  2. {
  3.     return ($_SERVER['REQUEST_METHOD'] == 'POST');
  4. }

Saturday, December 6, 2008

Summation

This week's function is summation using the Gauss method.


ASP

  1. function sum(x, y)
  2.     sum = (x + y) * ((y - x + 1) / 2)
  3. end function

PHP

  1. function sum($x, $y)
  2. {
  3.     return ($x + $y) * (($y - $x + 1) / 2);
  4. }