0

I have a DataTables table that loads data from

$table.dataTable({
    ajax: 'path/to/getData.php',
});

getData.php makes some database calls and returns json_encode() of the output array and DataTables is able to parse it just fine.

However if someone was to go to http://mywebsite.com/path/to/getData.php, they would be able to see all of the raw JSON data and potentially scrape it.

Is there a way to prevent people from accessing getData.php unless it is called by dataTables?

I'm fairly certain this will have to be a modification to the PHP code since anyone could potentially see my workaround via Javascript.

3
  • You can avoid using Ajax in your web page by using HTML sourced data instead. Use PHP to build the <html> table's contents, and remove the ajax option from your DataTable definition. There are probably several different ways to do this - but here is one. See also HTML (DOM) sourced data. The end user will only have access to one page of data at a time - and will need to scrape it from the HTML instead of getting it all from a JSON response. Commented Nov 4, 2021 at 22:34
  • I was originally doing that but I have ~5000 rows and it was taking DataTables a bit of time to process everything. Doing ajax call was much faster. Commented Nov 4, 2021 at 22:54
  • I like your solution. As a side note, it helps if you tell us in your question what you have already tried - we won't suggest something that you already know is not suitable. Commented Nov 4, 2021 at 23:08

1 Answer 1

1

I ended up using $_SESSION variables.

At the start of the page that contains the table, I set $_SESSION['secure'] = true;

In my getData.php file, I have:

if($_SESSION['secure']) {
    echo json_encode($output);
    $_SESSION['secure'] = false;
}

To reset the secure session variable.

Sign up to request clarification or add additional context in comments.

Comments

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.