Last active
August 30, 2021 09:43
-
-
Save relliv/4d6dffa00cff290c25b3feed5f88f490 to your computer and use it in GitHub Desktop.
Secure GET & POST Methods
This file contains 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 | |
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 */ | |
} |
This file contains 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 | |
$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