Need help with cookies

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][pre]<?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()+606024*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);
}
}

?>

<?php if (isset($_POST['ok'])){ if($_POST['mail']== $_COOKIE['email']){ echo "This email address has already been registered
"; }else { echo "

Thank you!

"; } } ?> <?php if (!empty($_COOKIE["sevendays"])) echo "only in 7".$_COOKIE["sevendays"]." days!"; if ($_COOKIE["sevendays"]) { echo $_COOKIE["sevendays"]; ?>

Not my email: <? echo $_COOKIE["sevendays"]; ?>?

<?php }else { ?>

Please register

Name: <? if ($_COOKIE['name']) { echo $_COOKIE['name']; } else {

?>

<?php }

?>

Email: <? if ($_COOKIE['email']) { echo $_COOKIE['email']; } else { ?> <?php }

?>

I accept terms and conditions.
<?php } ?> [/pre][/php]

also, i tried this one:

[php][pre]<?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);
?>
[/pre][/php]

How can i code something that each user must register her email address in order to download the file. 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.

Sponsor our Newsletter | Privacy Policy | Terms of Service