It's been quite a while since I've posted PHP code. I tend to avoid posting code in that language since it just too easy of a target: PHP is used primarily by hobbyists and gives you just enough rope to shoot yourself with (need I say more than Variable Variables)? No less, I thought this code was worth sharing.

Submitted anonymously from a patient billing system, the original coder had some rather interesting programming techniques. He didn't quite believe in using variables, so he opted to store everything in the intrinsic Session object. Same thing with all fields in the Post and Get objects; those were always dumped into the Session as well. He just really liked the session. But what really struck me as ... interesting ... about the code was the handling of the "doaction" command submitted by the user ...

<?php
  ################################################################################
  ############### Functions to perform the actions ###############################
  ################################################################################

  /* ED: Dump the request (form or querystring) hash into the session */
  if ($_POST) { $_SESSION['VAR']=$_POST; } elseif ($_GET) { $_SESSION['VAR']=$_GET; }
  
  /* ED: ... snip ... */

  /* ED: display value of all form elements named "dispdata" */
  if ($dumps[$_SESSION['VAR']['dispdata']][0])
  {
    print $dumps[$_SESSION['VAR']['dispdata']][0]; 
    print "<hr noshade color=\"#FFFFFF\" size=\"1\">";
  }

  /* ED: If updatedata was a form element, update the data and clear any other commands */
  if (isset($_SESSION['VAR']['updatedata']))
  { 
    data_update();
    print "<hr noshade color=\"#FFFFFF\" size=\"1\">";
    unset($_SESSION['VAR']['adddata']);
    unset($_SESSION['VAR']['editdata']);
    unset($_SESSION['VAR']['updatedata']);
  }

  /* ED: If not updating, process other commands */
  if (isset($_SESSION['VAR']['adddata']))
  { 
    data_add();
  } elseif (isset($_SESSION['VAR']['editdata'])) { 
    data_edit();
  } elseif (isset($_SESSION['VAR']['dispdata'])) {
    data_display();
  } elseif (isset($_SESSION['VAR']['doaction'])) {
    /* ED: Call Function specified on form with given parameter. */
    $_SESSION['VAR']['doaction']($_SESSION['VAR']['forclient']);
  } else { 
    print "&nbsp;"; 
  }
?>
[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!