Jump to content

Recommended Posts

Hi there. I'm really not very savvy to PHP, but I was assigned maintaining a staff website that partially uses PHP.

 

I was asked to make a search function on the site. I tried copy/pasting the code for a search button that was on another portion of the site. It works okay, but I can't figure out how to make it ignore case when searching. I've figured out how to change whether it automatically corrects case, but that doesn't help as not all the files have the same case.

 

Here's the code that we have on there:

 

        <?php
$bResults = false;
$search = $_POST['search'];
$searchChunks = explode(" ", $search);
for($i = 0; $i < count($searchChunks); $i++)



foreach (glob("{*/*$searchChunks[$i]*.pdf,*/*/*$searchChunks[$i]*.pdf,*/*$searchChunks[$i]*.docx,*/*/*$searchChunks[$i]*.docx}", GLOB_BRACE) as $filename)
{
echo "<a href=\"".$filename."\">".$filename."</a><p>";
$bResults = true;
}

if (!$bResults)
{
printf("No matches for search criteria '%s'.\n", $search);
}
?>

The code lets anybody search the entire disk for documents, including the ones which you definitely don't want to see on the Internet. That's not very clever. Even if nobody cares about the website, they will care about major data leaks.

 

So don't be naive. Inserting raw user input into file paths is the last thing you should do. If there aren't too many files, just iterate over all documents in the specific subfolders (with a fixed pattern) and compare the file names with the user-provided search term. This also fixes the problem of case sensitivity, because you can use any comparison method you want.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.