Skip to content

Instantly share code, notes, and snippets.

@relliv
Last active August 30, 2021 09:43
Show Gist options
  • Save relliv/4d6dffa00cff290c25b3feed5f88f490 to your computer and use it in GitHub Desktop.
Save relliv/4d6dffa00cff290c25b3feed5f88f490 to your computer and use it in GitHub Desktop.
Secure GET & POST Methods
<?php
class Common
{
/*--------------------- $_GET & $_POST ----------------- START */
/**
* Get $_GET content, array or array item in filtered way
*
* @param object $parameter could be array or array item
*/
public function get($parameter = null)
{
if ($parameter == null) {
if (is_array($_GET)) {
$request = array();
foreach ($_GET as $key => $param) {
$request[$key] = strip_tags(trim(addslashes(htmlspecialchars($param))));
}
return $request;
}
} else if (isset($_GET[$parameter])) {
return strip_tags(trim(addslashes(htmlspecialchars($_GET[$parameter]))));
}
return false;
}
/**
* Get $_POST content, array or array item in filtered way
*
* @param object $parameter could be array or array item
*/
public function post($parameter = null)
{
if ($parameter == null && $_POST) {
if (is_array($_POST)) {
$request = array();
foreach ($_POST as $key => $param) {
$request[$key] = htmlspecialchars(addslashes(trim($param)));
}
return $request;
}
} else if (!empty($_POST[$parameter])) {
if (is_array($_POST[$parameter])) {
$request = array();
foreach ($_POST[$parameter] as $param) {
$request[] = htmlspecialchars(addslashes(trim($param)));
}
return $request;
}
return htmlspecialchars(addslashes(trim($_POST[$parameter])));
}
return false;
}
/*--------------------- $_GET & $_POST ----------------- END */
}
<?php
$common = new Common();
// returns $_GET array with all contains
$common->get();
// get single $_GET parameter value
$common->get('some_parameter');
// returns $_POST array with all contains
$common->post();
// get single $_POST parameter value
$common->post('some_parameter');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment