| classVars.php |
|---|
• Overview:
The purposes of this class are to retrieve command line arguments (1) from the right end of the URL by which the current web page was invoked by a website visitor's browser; and (2) from the body of the web page itself.
As explained in documentation for classRequest.php (in the previous entry in the Table of Contents sidebar at left), if the visitor's web page invokes the target web page by using a <form> construct with GET method, then classVars.php cannot be used in tandem with classRequest.php (to retrieve aliases in the URL) because the alias list is assumed to be part of the last command line argument retrieved by classVars.php. However, the two classes can be used in tandem if the POST method is used, because classRequest.php compensates for inclusion of a web page name in what would normally be the first position in an alias list.
Two scripts are required to validate correct functioning of classVars.php running in tandem with classRequest.php. The first script is the HTML page that creates preconditions for the test within a <form> ... </form> element:
<html>
<!--
filename: varsTest1.html
author: Al Olmstead, AlOlmstead.com, Al@AlOlmstead.com
-->
<head>
<title>varsTest1.html</title>
</head>
<body>
<form name="testForm" method="post"
action="varsTest2.php">
<input type="text" name="site" value="joe"><br>
<input type="text" name="page" value="home"><br>
<input type="text" name="action" value="login"><br>
<input type="text" name="username" value="mike"><br>
<input type="text" name="password1" value="X"><br>
<input type="text" name="password2" value="X"><br>
<input type="submit" value="Send">
</form>
</body>
</html>
The second script is a PHP script that retrieves all possible aliases and command line variables using first classRequest.php and then classVars.php:
<?
/*
filename: varsTest2.php
author: Al Olmstead, AlOlmstead.com, Al@AlOlmstead.com
*/
require("classRequest.php");
$request=new Request();
$printLine=
"<br>SubDomain=".$request->getSubDomain().
"<br>DomainName=".$request->getDomainName().
"<br>Domain=".$request->getDomain().
"<br>Site=".$request->getSite().
"<br>Page=".$request->getPage().
"<br>Action=".$request->getAction().
"<br>Arg1=".$request->getArg1().
"<br>Arg2=".$request->getArg2().
"<br>Arg3=".$request->getArg3();
print($printLine);
require("classVars.php");
$vars=new Vars();
$varList="site;page;action;username;password1;password2";
$vars->declareVars($varList);
$vars->retrieveVars($varList);
$printLine=
"<br>site=".$vars->getVar("site").
"<br>page=".$vars->getVar("page").
"<br>action=".$vars->getVar("action").
"<br>username=".$vars->getVar("username").
"<br>password1=".$vars->getVar("password1").
"<br>password2=".$vars->getVar("password2");
print($printLine);
?>
The domain used for testing is "DroidPals.com". The first script is invoked by
http://DroidPals.com/varsTest1.html
which produces the following screen display:

Clicking "Send" causes varsTest2.php to produce the following screen display:

Notice that there is no subdomain value because "DroidPals.com" was invoked without "www." at the front. However, now insert "www." and duplicate command line arguments in a list of aliases as follows:
<html>
<!--
filename: varsTest1.html
author: Al Olmstead, AlOlmstead.com, Al@AlOlmstead.com
-->
<head>
<title>varsTest1.html</title>
</head>
<body>
<form name="testForm" method="post"
action="varsTest2.php/joe/home/login/mike/X/X">
<input type="text" name="site" value="joe"><br>
<input type="text" name="page" value="home"><br>
<input type="text" name="action" value="login"><br>
<input type="text" name="username" value="mike"><br>
<input type="text" name="password1" value="X"><br>
<input type="text" name="password2" value="X"><br>
<input type="submit" value="Send">
</form>
</body>
</html>
In response to these changes,varsTest2.php produces the following display:

In other words, by using the POST method in a <form> element, we are able to communicate identical data to the server by two completely different means. Of course, there is no practical point to duplication; but the test shows that different combinations of aliases and POST variables make great variability possible in website structure and operation.
As the following illustrates, these two classes operate quite differently. classRequest.php performs its functions automatically immediately upon instantiation. That is possible because URL structure is uniform throughout the Internet. The class doesn't need to be told what to do because it already knows what to do. In contrast, classVars.php waits to be told what to do because there is no such thing as standard arguments anywhere across the Internet. All GET and POST arguments are unique to each <form> element in every page.
• Invocation:
• Declare variable list:
• Retrieve variables from browser to server:
• Display list of variables:
It is not necessary to declare and retrieve all variables at the same time. Commonly, doing so is impractical because possible values of argument #2 may depend upon the actual value of argument #1, and so on down the list of possible arguments. However, it is a good idea to always declare and retrieve any argument in immediate succession. If an argument is merely declared but not retrieved from the browser to the server, then its value remains in its initial state, empty.
Bear in mind as well that, unlike in computer programming, command line arguments are always type character (text) no matter their appearance. PHP will automatically convert text to numeric based upon usage context; however, scripts must explicitly convert text to Boolean (true/false) because $item=true is never the same as $item="true".
Finally, notice that the PHP script that receives data has no need to know if it was sent by GET or POST method. If the class discovers that a unit of declared data was not sent by GET, then it always checks to see if it was sent by POST instead.
• Complete source listing of classVars.php:
<?
/*
filename: classVars.php
author: Al Olmstead, AlOlmstead.com, Al@AlOlmstead.com
dependencies: none
*/
class Vars {
var $varsArray;
function declareVars($varNameList) {
$varsKeywordPtr=0;
$varsValuePtr=1;
$varsArrayPtr=count($this->varsArray)-1;
$workArray=explode(";",trim($varNameList));
$workArrayCount=count($workArray);
for($x=0; $x<$workArrayCount; $x++) {
$duplicateFlag=
$this->findKeyword(strtolower($workArray[$x]));
if($duplicateFlag<0) {
$this->varsArray[++$varsArrayPtr]=array();
$this->varsArray[$varsArrayPtr][$varsKeywordPtr]=
strtolower(trim($workArray[$x]));
$this->varsArray[$varsArrayPtr][$varsValuePtr]=
"";
} // if()
} // for()
} // declareVars()
function findKeyword($keywordToFind) {
$keyword=strtolower(trim($keywordToFind));
$varsArrayCount=count($this->varsArray);
$varsKeywordPtr=0;
$returnValue=-1;
for($x=0; $x<$varsArrayCount; $x++) {
if ($this->varsArray[$x][$varsKeywordPtr]==
$keyword) {
$returnValue=$x;
break;
} // if()
} // for()
return($returnValue);
} // findKeyword()
function retrieveVars($varNameList) {
$varsArrayPtr=-1;
$varsValuePtr=1;
$varsArrayCount=count($this->varsArray);
$workArray=explode(";",$varNameList);
$workArrayCount=count($workArray);
for($x=0; $x<$workArrayCount; $x++) {
$varsArrayPtr=
$this->findKeyword(trim($workArray[$x]));
if($varsArrayPtr>-1) {
if(isset($_GET[trim($workArray[$x])])) {
$this->varsArray[$varsArrayPtr]
[$varsValuePtr]=$_GET[trim($workArray[$x])];
} else {
if(isset($_POST[trim($workArray[$x])])) {
$this->varsArray[$varsArrayPtr]
[$varsValuePtr]=
$_POST[trim($workArray[$x])];
} // if()
} // else{}
} // if{}
} // for()
} // retrieveVars()
function getVar($varKeyword) {
$returnLine="";
$varsValuePtr=1;
$varsArrayPtr=$this->findKeyword(trim($varKeyword));
if($varsArrayPtr>-1) {
$returnLine=
$this->varsArray[$varsArrayPtr][$varsValuePtr];
} // if()
return($returnLine);
} // getVar()
function __construct() {
$this->varsArray=array();
} // __construct()
} // class Vars{}
?>