note 38019 deleted from language.operators.bitwise by cmb
| From: | cmb@php.net | Date: | Sun, 20 Oct 2019 08:50:31 +0000 |
| Subject: | note 38019 deleted from language.operators.bitwise by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-212698@lists.php.net to get a copy of this message | ||
Note Submitter: krang at krang dot org dot uk
----
Hopefully this may help someone understand the fun of Bitwise Operators....
The purpose of this function is to return a value from the GPC (Get, Post and Cookie) and do some
basic formatting to it depending on the $VALIDATION value:
<?PHP
function RETURN_SUBMITTED_VALUE ($VARIABLE, $METHOD, $VALIDATION) {
//-------------------------------
// Get the value from the
// relevant submit method...
if ($METHOD == 'POST') {
if (!isset($_POST[$VARIABLE])) $_POST[$VARIABLE] = '';
$VALUE = $_POST[$VARIABLE];
} elseif ($METHOD == 'COOKIE') {
if (!isset($_COOKIE[$VARIABLE])) $_COOKIE[$VARIABLE] = '';
$VALUE = $_COOKIE[$VARIABLE];
} else {
if (!isset($_GET[$VARIABLE])) $_GET[$VARIABLE] = '';
$VALUE = $_GET[$VARIABLE];
}
//-------------------------------
// If necessary strip the slashes.
// the "GPC" means - GET, POST
// COOKIE.
if (ini_get ('magic_quotes_gpc') == true) {
$VALUE = stripslashes($VALUE);
}
//-------------------------------
// Now for the different types
// of VALIDATION's
if (($VALIDATION & 8) == 8) {
$VALUE = (int)$VALUE;
}
if (($VALIDATION & 4) == 4) {
$VALUE = strtolower($VALUE);
}
if (($VALIDATION & 2) == 2) {
$VALUE = strip_tags($VALUE);
}
if (($VALIDATION & 1) == 1) {
$VALUE = trim($VALUE);
}
//-------------------------------
// Finally return the value
return $VALUE;
}
echo RETURN_SUBMITTED_VALUE ('ID', 'GET', 8) . '<br />';
// Convert to an Integer
echo RETURN_SUBMITTED_VALUE ('NAME', 'GET', 3) . '<br />';
// Trim Whitespace and Strip HTML tags
echo RETURN_SUBMITTED_VALUE ('GENDER', 'GET', 6) . '<br />';
// Strip HTML tags and convert to lower case
//-----------------------------------------------
// If this script was loaded under the URL
// index.php?ID=19a&NAME=Krang&GENDER=MalE
// it would print
//
// 19
// Krang
// male
//-----------------------------------------------
?>
For those that dont understand binary, the numbers you see are not random, they double each time
(1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024...) which allows you to mix and match the different
function, eg...
1 + 2 = 3 (Trim Whitespace + Strip HTML)
2 + 4 = 6 (Strip HTML + Convert to lower case)