Home > Blockchain >  make background Countdown in PHP then echo the string
make background Countdown in PHP then echo the string

Time:06-22

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!

CodePudding user response:

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.

CodePudding user response:

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 = "none";
}, 15000);
  • Related