0

I am creating a download page but users can just inspect elements to get the direct link. I can't think of any other solution except for this: PHP echo the direct link after 15 seconds.

But the problem is PHP loads before the HTML and javascript. How can I make PHP run in the background while HTML and Javascript are running?

I tried to do the following code below using javascript and ajax but again, if they inspect elements they will see a direct link. But in PHP, if they inspect elements, they will not see the direct link.

This is a sample code:

<p>This paragraph should show before 10 seconds.</p>

<!-- a tag below with innerText of link should show after 10 seconds -->
<a id="test"></a>

<?php

sleep(10);
echo '<script>
document.getElementById("test").innerText = "link";
</script>';

?>

Sorry, I am new to PHP, Thank you!

  • I highly recommend that you read my answer to [this question](https://stackoverflow.com/questions/38509334/full-secure-image-upload-script/38712921#38712921). Although it's about file upload, it also includes a download functionality part. It'll help you solve the issue entirely as PHP sends the file directly without giving any download link at all. Besides that, it'll also teach you something about file security – icecub Jun 21 '22 at 08:10
  • Before continuing, please read also [this post](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming). The task at hands doesn't need PHP, you can use a DOM [timer](https://developer.mozilla.org/en-US/docs/Web/API/setTimeout) instead. What comes to the content of a document, you can't hide anything, what is loaded on the page, is visible to all users. – Teemu Jun 21 '22 at 08:21

2 Answers2

0

Aside from my comment to your question, you should know that Ajax is not suitable for file downloads. Instead, you should use PHP to directly send the file instead of a hyperlink:

/* Get the original filename */
$fileDir = './your/file/directory';
$file = 'file.txt';

/* Send headers and file to visitor */
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($fileDir.'/'.$file));
header("Content-Type: application/octet-stream");
readfile($fileDir.'/'.$file);

Then just create a hyperlink to that PHP file:

<!-- Obviously change the href attribute to where ever you save the above code -->
<a href="./downloadFile.php">Download</a>

The visitor will be able to see the download.php file link, but they can't see which file it sends or where that file is coming from.

Edit:

I should probably note that this is only useful in case you want to prevent direct hotlinking towards the file. Additional security measures will need to be taken to prevent direct hotlinking towards this PHP file as well. Like setting up a session and blocking access in case the session isn't set.

icecub
  • 8,615
  • 6
  • 41
  • 70
0

If I understand your question. You want to echo string after 15 sec. Try with Javascript.

<a id="test" style="display:none">link</a>

setTimeout(function(){ 
document.getElementById("test").style.display = "block";
}, 15000);
Codeshua
  • 1
  • 3