note 129109 added to function.array-intersect
| From: | theking2 at main dot php dot net | Date: | Sat, 16 Dec 2023 18:21:03 +0000 |
| Subject: | note 129109 added to function.array-intersect | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-221063@lists.php.net to get a copy of this message | ||
A more generic parameter checking:
<?php
/**
* Check if all required parameters are present in the request
*
* @param mixed $request any of $_GET, $_POST, $_REQUEST
* @param mixed $required array of required parameters
* @return bool true if all required parameters are present
*/
function check_params( array $request, array $required ): bool
{
$check = array_intersect( array_keys( $request ), $required );
return count( $check ) === count( $required );
}
?>
Use this like
<?php
if( !check_params( $_GET, ['cust_id','prod_id'] ) {
header( "HTTP/1.1 400 Bad Request" );
echo "Bad Request";
exit();
}
?>
Let me know if something is build in PHP already...
----
Server IP: 45.112.84.4
Probable Submitter: 45.91.157.134 (proxied: 185.163.92.217)
----
Manual Page -- https://php.net/manual/en/function.array-intersect.php
Edit -- https://main.php.net/note/edit/129109
Del: integrated -- https://main.php.net/note/delete/129109/integrated
Del: useless -- https://main.php.net/note/delete/129109/useless
Del: bad code -- https://main.php.net/note/delete/129109/bad+code
Del: spam -- https://main.php.net/note/delete/129109/spam
Del: non-english -- https://main.php.net/note/delete/129109/non-english
Del: in docs -- https://main.php.net/note/delete/129109/in+docs
Del: other reasons-- https://main.php.net/note/delete/129109
Reject -- https://main.php.net/note/reject/129109
Search -- https://main.php.net/manage/user-notes.php