2
votes

We are currently performing searches on a Database and returning results in JSON format to use on a Google Maps. The file that we call is named getvenues.php and works great on the server. It accepts a number of parameters and returns the results based on the query.

We then have a separate file that checks to see if there's a JSON file on the server which contains the results, matches its age against a setting, and then returns the data either from the cache file, or builds a new cache file if it's too old.

Since there are several thousand possible search options, we only cache single searches (either on a County, Region or Type). The JavaScript always calls our search_cache_builder.php file. If there is more than one search parameter, the file simply gets the contents returned by getvenues.php and serves it up without any caching.

Everything works great except for one particular combination. If a search is run for venue_type=Castle / Fort and venue_name=Leeds Castle, the search_cache_builder.php returns an empty array, even though accessing getvenues.php directly returns the required data.

Here's a sample of the getvenues.php working with this data > http://dev.weddingvenues.com/lincweb/getvenues.php?type=Castle%20/%20Fort&venue_name=Leeds%20Castle

And here's what the search_cache_builder.php script returns with an identical search (the address we are sending to is correct) > http://www.weddingvenues.com/search_cache_builder.php?type=castle%20/%20fort&venue_name=Leeds%20Castle

Here's the code for the search_cache_builder.php file, which relates to this particular query:

$get_string = '?';
foreach($_GET as $key => $value)
{
    if($get_string === '?')
    {
        $get_string .= $key . '=' . $value;
    }
    else
    {
        $get_string .= '&' . $key . '=' . $value;
    }
}

//$get_string = str_replace(' ', '', $get_string);

// Otherwise, we need to serve up the page as is:
$file_url = GET_VEN_URL . $get_string;
echo file_get_contents($file_url);

Can anyone offer an explanation as to why the search_cache_builder.php file is returning an empty array?

3

3 Answers

1
votes

You should urlencode() your parameter values.

In fact, while your getvenues.php receives parameters directly from a browser it behaves OK, 'cause they are correctly urlencoded.

I tried what follows in my computer towards your service and it works:

define ("GET_VEN_URL", "http://www.weddingvenues.com/getvenues.php");

$get_string = '?';
foreach($_GET as $key => $value)
{
    if($get_string === '?')
    {
        $get_string .= $key . '=' . urlencode($value);
    }
    else
    {
        $get_string .= '&' . $key . '=' . urlencode($value);
    }
}


$file_url = GET_VEN_URL . $get_string;
echo "<pre>";
echo $file_url;
echo "<pre>";

echo file_get_contents($file_url);
0
votes

Because $get_string === '?' is ALWAYS false, change it to ==, ? is not a boolean.

I would not recommend using GET parameters, &,? as part of the file name.
Beside this, it will quickly hit into 5k (or 4k, can not recall) limit for a file name.

You can do a sort by $GET, md5 (or hash) the array to a random string.
As long you ensure the hashing mechanism is consistent, you can easily retrieve the cache file.

0
votes

First try to urldecode values, then use http_build_query to generate your get_string, your problem must be the special chars in values (like /).

Edit: If you change the order it works: http://www.weddingvenues.com/search_cache_builder.php?venue_name=Leeds%20Castle&type=castle%20/%20fort