Hello. I need your help! I have hometask and i am not able to solve it and i tried a lot of things.
My hometask says: each user must register own email address to download the file. You must use cookies to detect whether the user has already registered, and to ensure that the user downloads the file only once within 7 days of registering.
I tried this code:
<?php
if (!empty($_POST['delete_cookie'])) {
setcookie("sevendays", "", time()-3600);
}
if(isset($_POST['terms'])AND(isset($_POST['email']))AND(empty($_COOKIE["sevendays"]))){
$email = $_POST['email'];
setcookie("sevendays", "email", time()+60*60*24*7);
$filepath = $_SERVER['DOCUMENT_ROOT']."/.php_files/acme_brochure.pdf";
if (file_exists($filepath)) {
header("Content-Type: application/force-download");
header("Content-Disposition:filename=\"brochure.pdf\"");
$fd = fopen($filepath,'rb');
fpassthru($fd);
fclose($fd);
}
}
?>
and this one:
<?php
// Considering form is posted here
// And your code should be as below:
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
if(isset($_POST['email']) && $_POST['email'])
{
if(isset($_COOKIE['email']) && count($_COOKIE['email'])>0)
{
if(in_array($_POST['email'],$_COOKIE['email']))
{
// Email Exist In cookies
// Show download link
}
else
{
// Email not exist in cookies or may be cookies
expired
}
}
else
{
// Not even a single email set in cookies.
}
}
}
// Your cookies saving code should be as below:
// It will create a array of email if you save like this.
setcookie("email[]", $value, (time()+3600*24)*7);
?>