Ah-HA! It was tricky (and I'm a dumbass for editing the wrong file while gnawing with the desired approach) but I figured it out...sort of.
It's some sort of reverse-variable scope. It seems that variables defined outside of a function should probably be passed as parameters in the situation I'm dealing with.
I commented out lines 57~63 and manually entered the regex int he code below and everything worked fine...
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>PHP Serverside Validation of CSS post data</title>
<style type="text/css">
body,html {font-family: monospace;}
b {color: #00f;}
b.bad {color: #f00;}
b.good {color: #0f0;}
div {outline: #f00 solid 1px;}
div.overflow {float: left; font-size: 12px; height: 240px; margin: 4px; overflow: auto; width: 25%;}
form {float: left; width: 400px;}
form input {font-size: 10px; width: 60px;}
</style>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<fieldset>
<div>
<input name="ce000" value="body" />
<input name="ce001" value="444" />
<input name="ce002" value="11/002" />
<input name="ce003" value="00f" />
<input name="ce004" value="00f" />
</div>
<div>
<input name="ce010" value="h2, h2, h3, h4" />
<input name="ce011" value="transparent" />
<input name="ce012" value="none" />
<input name="ce013" value="1f1" />
<input name="ce014" value="115" />
</div>
<div>
<input name="ce020" value="div" />
<!-- Default Value below is invalid, add an extra digit (0-F Hex) to validate -->
<input name="ce021" style="background-color: #000; border-color: #f0f; color: #fff;" value="55555" />
<input name="ce022" value="01/002" />
<input name="ce023" value="5f6" />
<input name="ce024" value="5f6" />
</div>
<div>
<input name="ce030" value="span" />
<input name="ce031" value="7d6" />
<input name="ce032" value="" />
<input name="ce033" value="" />
<input name="ce034" value="" />
</div>
<br style="clear: both;" />
<input style="display: block; width: 60%;" type="submit" value="Validate Form Data" />
</fieldset>
</form>
<?php
function super_validate()
{
/*
if (validate_clientside_array($regex_0_selectors,'0') != 'invalid'
&& validate_clientside_array($regex_1_colors,'1') != 'invalid'
&& validate_clientside_array($regex_2_bgimages,'2') != 'invalid'
&& validate_clientside_array($regex_1_colors,'3') != 'invalid'
&& validate_clientside_array($regex_1_colors,'4') != 'invalid'
*/
if (validate_clientside_array('/([0-9A-z]([#|.|,|:][0-9A-z]){0,10})?$/','0') == 'invalid'
||
validate_clientside_array('/^(?:(?:[0-9a-f]{3}){1,2}|transparent)?$/','1') == 'invalid'
||
validate_clientside_array('/^(([0-9]{2})\/([0-9]{3})|none)?$/','2') == 'invalid'
||
validate_clientside_array('/^(?:(?:[0-9a-f]{3}){1,2}|transparent)?$/','3') == 'invalid'
||
validate_clientside_array('/^(?:(?:[0-9a-f]{3}){1,2}|transparent)?$/','4') == 'invalid'
) {return 'invalid';}
}
function validate_clientside_array($regex, $position)
{
$item = '/'.$position.'$/';
//echo 'parameter 1 = <b>'.$regex.'</b><br />';
//echo 'parameter 3 = <b>'.$position.'</b><br /><br />';
foreach($_POST as $key => $value)
if (preg_match($item, $key))
{
//if (preg_match($regex, $value)) {echo ' <b class="good">'.$key.' = '.$value.'</b> is a <b class="good">match</b>!</b><br />'."\n";}
//else {echo '<b class="bad">'.$key.' == '.$value.'</b> not a <b class="bad">match</b>!</b><br />'; $validity = '1'; return 'not valid';}
if (!preg_match($regex, $value)) {return 'invalid';}
/*return false;*/
}
}
$regex_0_selectors = '/([0-9A-z]([#|.|,|:][0-9A-z]){0,10})?$/';
$regex_1_colors = '/^(?:(?:[0-9a-f]{3}){1,2}|transparent)?$/';
$regex_2_bgimages = '/^(([0-9]{2})\/([0-9]{3})|none)?$/';
echo '<b> R E S U L T = </b>'.super_validate();
echo '<br style="clear: both;" />';
echo '<b> RESULT = </b> '.validate_clientside_array($regex_0_selectors,'0').'<br />';
echo '<b> RESULT = </b> '.validate_clientside_array($regex_2_bgimages,'2').'<br />';
echo '<b> RESULT = </b> '.validate_clientside_array($regex_1_colors,'1').'<br />';
echo '<b> RESULT = </b> '.validate_clientside_array($regex_1_colors,'3').'<br />';
echo '<b> RESULT = </b> '.validate_clientside_array($regex_1_colors,'4').'<br />';
if (validate_clientside_array($regex_0_selectors,'0') != 'invalid'
&& validate_clientside_array($regex_1_colors,'1') != 'invalid'
&& validate_clientside_array($regex_2_bgimages,'2') != 'invalid'
&& validate_clientside_array($regex_1_colors,'3') != 'invalid'
&& validate_clientside_array($regex_1_colors,'4') != 'invalid'
) {echo '<br />The array contains <b class="good">only valid data</b>.';}
else {echo '<br />The array <b class="bad">contains invalid data</b>!';}
?>
</body>
</html>
So I passed the variables as parameters and reestablished the variables outside the function as variables within the super function.
On line 102 I define the parameters as the outside variables (the variables defined on lines 98~100).
Then inside of the parent function at line 55 I essentially redefined the variables.
It seems sort of a waste though?
So I'm open to suggestions: would it be better just to compare each function as I do on lines 110~114?
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>PHP Serverside Validation of CSS post data</title>
<style type="text/css">
body,html {font-family: monospace;}
b {color: #00f;}
b.bad {color: #f00;}
b.good {color: #0f0;}
div {outline: #f00 solid 1px;}
div.overflow {float: left; font-size: 12px; height: 240px; margin: 4px; overflow: auto; width: 25%;}
form {float: left; width: 400px;}
form input {font-size: 10px; width: 60px;}
</style>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<fieldset>
<div>
<input name="ce000" value="body" />
<input name="ce001" value="444" />
<input name="ce002" value="11/002" />
<input name="ce003" value="00f" />
<input name="ce004" value="00f" />
</div>
<div>
<input name="ce010" value="h2, h2, h3, h4" />
<input name="ce011" value="transparent" />
<input name="ce012" value="none" />
<input name="ce013" value="1f1" />
<input name="ce014" value="115" />
</div>
<div>
<input name="ce020" value="div" />
<!-- Default Value below is invalid, add an extra digit (0-F Hex) to validate -->
<input name="ce021" style="background-color: #000; border-color: #f0f; color: #fff;" value="55555" />
<input name="ce022" value="01/002" />
<input name="ce023" value="5f6" />
<input name="ce024" value="5f6" />
</div>
<div>
<input name="ce030" value="span" />
<input name="ce031" value="7d6" />
<input name="ce032" value="" />
<input name="ce033" value="" />
<input name="ce034" value="" />
</div>
<br style="clear: both;" />
<input style="display: block; width: 60%;" type="submit" value="Validate Form Data" />
</fieldset>
</form>
<?php
function super_validate($regex_0_selectors, $regex_1_colors, $regex_2_bgimages)
{
$regex_0_selectors = $regex_0_selectors;
$regex_1_colors = $regex_1_colors;
$regex_2_bgimages = $regex_2_bgimages;
if (validate_clientside_array($regex_0_selectors,'0') != 'invalid'
&& validate_clientside_array($regex_1_colors,'1') != 'invalid'
&& validate_clientside_array($regex_2_bgimages,'2') != 'invalid'
&& validate_clientside_array($regex_1_colors,'3') != 'invalid'
&& validate_clientside_array($regex_1_colors,'4') != 'invalid'
/*
if (validate_clientside_array('/([0-9A-z]([#|.|,|:][0-9A-z]){0,10})?$/','0') == 'invalid'
||
validate_clientside_array('/^(?:(?:[0-9a-f]{3}){1,2}|transparent)?$/','1') == 'invalid'
||
validate_clientside_array('/^(([0-9]{2})\/([0-9]{3})|none)?$/','2') == 'invalid'
||
validate_clientside_array('/^(?:(?:[0-9a-f]{3}){1,2}|transparent)?$/','3') == 'invalid'
||
validate_clientside_array('/^(?:(?:[0-9a-f]{3}){1,2}|transparent)?$/','4') == 'invalid'
*/
) {return 'invalid';}
}
function validate_clientside_array($regex, $position)
{
$item = '/'.$position.'$/';
//echo 'parameter 1 = <b>'.$regex.'</b><br />';
//echo 'parameter 3 = <b>'.$position.'</b><br /><br />';
foreach($_POST as $key => $value)
if (preg_match($item, $key))
{
//if (preg_match($regex, $value)) {echo ' <b class="good">'.$key.' = '.$value.'</b> is a <b class="good">match</b>!</b><br />'."\n";}
//else {echo '<b class="bad">'.$key.' == '.$value.'</b> not a <b class="bad">match</b>!</b><br />'; $validity = '1'; return 'not valid';}
if (!preg_match($regex, $value)) {return 'invalid';}
/*return false;*/
}
}
$regex_0_selectors = '/([0-9A-z]([#|.|,|:][0-9A-z]){0,10})?$/';
$regex_1_colors = '/^(?:(?:[0-9a-f]{3}){1,2}|transparent)?$/';
$regex_2_bgimages = '/^(([0-9]{2})\/([0-9]{3})|none)?$/';
echo '<b> R E S U L T = </b>'.super_validate($regex_0_selectors, $regex_1_colors, $regex_2_bgimages);
echo '<br style="clear: both;" />';
echo '<b> RESULT = </b> '.validate_clientside_array($regex_0_selectors,'0').'<br />';
echo '<b> RESULT = </b> '.validate_clientside_array($regex_2_bgimages,'2').'<br />';
echo '<b> RESULT = </b> '.validate_clientside_array($regex_1_colors,'1').'<br />';
echo '<b> RESULT = </b> '.validate_clientside_array($regex_1_colors,'3').'<br />';
echo '<b> RESULT = </b> '.validate_clientside_array($regex_1_colors,'4').'<br />';
if (validate_clientside_array($regex_0_selectors,'0') != 'invalid'
&& validate_clientside_array($regex_1_colors,'1') != 'invalid'
&& validate_clientside_array($regex_2_bgimages,'2') != 'invalid'
&& validate_clientside_array($regex_1_colors,'3') != 'invalid'
&& validate_clientside_array($regex_1_colors,'4') != 'invalid'
) {echo '<br />The array contains <b class="good">only valid data</b>.';}
else {echo '<br />The array <b class="bad">contains invalid data</b>!';}
?>
</body>
</html>