Let's say there is such a link

site.ru/page?url=1&obj=2&hr=3&blablabla=4

And there is an array of this kind:

$arr = array('url', 'obj'); 

How to check all variables from a GET request for existence? and if there is no variable in the array, output 404.

In this example, it should produce the 404th. The variables hr and blablabla do not exist.

  • 1 class if(!isset($_GET['url'])) redirect 404 - Vanya Avchyan
  • That you checked for the existence of the variable 'url' and not for the existence of third-party variables in the entire GET request. - vasa666
  • And I would even complicate the task so that, for example, a 'url' could not go after 'obj' or be repeated. Well, when between the source and serialized data one-to-one correspondence. - user239133
  • Yes, of course it would be nice)) But until I can even solve the problem with the main problem, namely the presence of unnecessary variables in the GET request. - vasa666
  • How else can you improve the formulation of the problem: $arr = array('url' => 'url', 'obj' => 'index') . Then you can use filters to check if the url and obj have adequate values. - user239133

1 answer 1

something like that?

 $allow = ['url', 'obj']; foreach($_GET as $param => $value){ if(!in_array($param, $allow)){ header("HTTP/1.0 404 Not Found"); exit; } } 

Another option is to calculate the difference of arrays:

 if(count(array_diff_key($_GET, $allow))){ .... // 404 } 
  • your code will output 404 even if url and obj exist. since a check is made on the existence of what cannot be in $allow - webDev_
  • so it is written? "and if there is no variable in the array, output 404". - teran
  • for some reason, there is an error with a complaint about this line $ allow = ['foto', 'obj']; - vasa666
  • @ vasa666 pkhp probably old, $allow = array('url', 'obj'); - teran
  • The first option earned!))) But this if (count (array_diff_key ($ _ GET, $ allow))) {.... // 404} for some reason, it cuts even with variables in the array - vasa666