skip to Main Content
parse_str($_SERVER['QUERY_STRING']);  

if ($m == ""){
  $dateComponents = getdate();
  $month = $dateComponents['mon'];
  $year = $dateComponents['year'];
} else {
  $month = $m;
  $year = $y;
}

echo build_previousMonth($month, $year, $monthString);
// ... etc

2

Answers


  1. Original implementation of parse_str() – and the particular way it was often used – was, to say the least, quite naive. The problem is that, when called without second argument, this function essentially allowed polluting the local symbol table. Here’s an extract of CVE Vulnerability Description:

    The parse_str function in (1) PHP, (2) Hardened-PHP, and (3) Suhosin,
    when called without a second parameter, might allow remote attackers
    to overwrite arbitrary variables by specifying variable names and
    values in the string to be parsed. NOTE: it is not clear whether this
    is a design limitation of the function or a bug in PHP, although it is
    likely to be regarded as a bug in Hardened-PHP and Suhosin.

    That’s why omitting second argument was deprecated in PHP 7.2 and dropped completely in PHP 8.0. Thus you need to reimplement this call so that the result is stored in a variable, and instead of checking $m, $y, … directly, you check elements of associative array stored in that variable instead.

    For example:

    parse_str($_SERVER['QUERY_STRING'], $query);
    if (empty($query['m'])) {
       // no data passed
    }
    else {
       $month = $query['m']; 
       // etc
    }
    

    As a sidenote, I’m really not sure why do you even have to parse query string, and not just use $_GET directly.

    Login or Signup to reply.
  2. For parse_str() requires two parameters one is input and another one is output

    Eg:

    $QUERY_STRING = "first=value&second=scvalue";
    
    parse_str($QUERY_STRING, $output_array)
    

    here $output_array contains the query string data as associate array and can be accessed like parameter name like $first_val = $output_array['first']

    Please check the link for parse_str() documentation https://www.php.net/manual/en/function.parse-str.php

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search