Created
January 30, 2012 20:00
-
-
Save samrdev/1706328 to your computer and use it in GitHub Desktop.
A very simple and dirty class to make the GET and POST variables safe.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// We need to include the class. | |
require('safe_class.php'); | |
// Here we specify the $_GET-variables as variables which can be used later. | |
list($foo, $bar) = safe::g('foo', 'bar'); | |
// We can do the same with the $_POST-variables and can also specify a boolean as return type! | |
// Here, the variable $signin will either be true or false. | |
list($username, $password, $signin) = safe::p('username', 'password', 'signin: bool'); | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// The safe class to make GETS and POSTS safe. | |
class safe { | |
public static function g() { | |
$g = func_get_args(); | |
return self::make($_GET, $g); | |
} | |
public static function p() { | |
$p = func_get_args(); | |
return self::make($_POST, $p); | |
} | |
private static function make(Array $t, Array $w) { | |
foreach ($w as $v) { | |
$v = explode(':', $v); | |
if (isset($v[1]) && (trim($v[1]) === 'bool' xor trim($v[1]) === 'boolean')) { | |
if (array_key_exists($v[0], $t)) { | |
$r[$v[0]] = true; | |
} else { | |
$r[$v[0]] = false; | |
} | |
} else { | |
if (array_key_exists($v[0], $t)) { | |
$r[$v[0]] = (string)$t[$v[0]]; | |
} else { | |
$r[$v[0]] = null; | |
} | |
} | |
} | |
return array_values($r); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment