2

I made this function to check for expected request variables. It was working great until I realized that if two values (Not keys) were the same, it would return a positive number as though a key was missing. Consider the following code:

function requestCheck($expectedAr)
{
    if(isset($_GET) && isset($_POST))
    {
        $requestAr = array_unique(array_merge($_GET, $_POST));
    }elseif(isset($_GET)){
        $requestAr = $_GET;
    }elseif(isset($_POST)){
        $requestAr = $_POST;
    }else{
        $requestAr = array();
    }
    $diffAr = array_diff_key(array_flip($expectedAr),$requestAr);
    if(count($diffAr) > 0)
    {
        returnError("Missing variables: ".implode(',',array_flip($diffAr)).".");
    }else {
        return $requestAr;
    }
}

$requestAr = requestCheck(['name','password']);

if 'name' and 'password' both hold the same value, it will run returnError(). Not seeing why.

Here's a dump of $_POST:

array (
  'poolName' => 'xpool',
  'userPrefix' => 'xpool'
)
2
  • Can you add some sample input arrays using var_export to dump them? Commented Apr 3, 2017 at 15:38
  • added the input array Commented Apr 3, 2017 at 15:46

1 Answer 1

1

array_unique will strip unique values so you'll end up with either name or password but not both.

Solution:

function requestCheck($expectedAr) {
    if(isset($_GET) && isset($_POST)) {
        $requestAr = $_REQUEST;
    }elseif(isset($_GET)) {
        $requestAr = $_GET;
    }elseif(isset($_POST)) {
        $requestAr = $_POST;
    }else{
        $requestAr = array();
    }
    $diffAr = array_diff_key(array_flip($expectedAr),$requestAr);
    if(count($diffAr) > 0)
    {
        returnError("Missing variables: ".implode(',',array_flip($diffAr)).".");
    }else {
        return $requestAr;
    }
}

$requestAr = requestCheck(['name','password']);

I think it's safe to also do the following:

function requestCheck($expectedAr) {

    $requestAr = isset($_REQUEST) && is_array($_REQUEST)?$_REQUEST:array();        
    $diffAr = array_diff_key(array_flip($expectedAr),$requestAr);

    if(count($diffAr) > 0)  {
        returnError("Missing variables: ".implode(',',array_flip($diffAr)).".");
    }else {
        return $requestAr;
    }
}

$requestAr = requestCheck(['name','password']);
Sign up to request clarification or add additional context in comments.

8 Comments

@xendi keys are unique by default. You can't have an array with the same key multiple times.
Well, I was combining post and get in case someone had sent it via both by mistake, I could accept it anyway. I was told to not use request for some reason. I think because it contains more than get and post. I'd run into an error using request before. Maybe with some reserved word.
@xendi $_REQUEST is more or less the same thing as combining the 2 (also includes cookies in there though).
Yeah, I think it was a session variable that was colliding, causing an error that lead me to stop using request.
array_intersect_key() will fix it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.