Skip to content

Instantly share code, notes, and snippets.

@jdbevan
Last active December 27, 2015 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdbevan/7336308 to your computer and use it in GitHub Desktop.
Save jdbevan/7336308 to your computer and use it in GitHub Desktop.
Proof of concept simple classes for escaping data and accessing SUPERGLOBALS with a little less pain
<?php
class Escape {
public static function forWebpage($data, $flags = ENT_QUOTES, $encoding = 'UTF-8') {
return htmlentities($data, $flags, $encoding);
}
public static function forMySQL($data, $dbcnx) {
return mysql_real_escape_string($data, $dbcnx);
}
public static function forMySQLi($data, $mysqli) {
return $mysqli->real_escape_string($data);
}
public static function forURL($data, $space_as_plus = true){
return $space_as_plus ? urlencode($data) : rawurlencode($data);
}
}
/*
* Deliberately no defaults for $unset as the required returned value if a
* variable is unset varies so much from case-to-case
*/
class Globals {
public static function GET($index, $unset) {
return isset( $_GET[$index] ) ? $_GET[$index] : $unset;
}
public static function POST($index, $unset) {
return isset( $_POST[$index] ) ? $_POST[$index] : $unset;
}
public static function SESSION($index, $unset) {
return isset( $_SESSION[$index] ) ? $_SESSION[$index] : $unset;
}
public static function SERVER($index, $unset) {
return isset( $_SERVER[$index] ) ? $_SERVER[$index] : $unset;
}
public static function COOKIE($index, $unset) {
return isset( $_COOKIE[$index] ) ? $_COOKIE[$index] : $unset;
}
public static function FILES($index, $unset) {
return isset( $_FILES[$index] ) ? $_FILES[$index] : $unset;
}
public static function REQUEST($index, $unset) {
return isset( $_REQUEST[$index] ) ? $_REQUEST[$index] : $unset;
}
public static function ENV($index, $unset) {
return isset( $_ENV[$index] ) ? $_ENV[$index] : $unset;
}
}
$name = "Jon Bevan's <span>code</span>\n\x00";
echo Escape::forWebpage($name), "<br>";
echo Escape::forURL($name, false), "<br>";
echo Escape::forMySQL($name, $dbcnx), "<br>";
echo Escape::forMySQLi($name, $mysqli), "<br>";
echo Escape::forWebpage( Globals::GET('name', '') ), "<br>";
echo Escape::forWebpage( Globals::SERVER('HTTP_HOST', '') ), "<br>";
echo Escape::forWebpage( Globals::POST('code', '') ), "<br>";
$account = Globals::SESSION('account_id', -1);
$https = Globals::SERVER('HTTPS', false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment