Jump to content

need help with cookies


david212

Recommended Posts

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 have been making a lot of things but i can get it working. Please help!

 

The last code i wrote is this:

 

<?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);
}
}

?>
<?php
if (isset($_POST['ok'])){

if($_POST['mail']== $_COOKIE['email']){
echo "This email address has already been registered<br>";
}else {
echo "<h3>Thank you!</h3>";
}
}
?>
<?php
if (!empty($_COOKIE["sevendays"])) echo "only in 7".$_COOKIE["sevendays"]." days!";
if ($_COOKIE["sevendays"]) {
echo $_COOKIE["sevendays"];
?>

<a href="download.php?delete_cookie=1">Not my email: <? echo $_COOKIE["sevendays"]; ?>?</a>
<?php
}else {
?>
<h3>Please register</h3>


<form method="POST" action="?">
<table>
<tr>
<td align="right">
Name:
</td>
<td align="left">
<?
if ($_COOKIE['name']) {
   echo $_COOKIE['name'];
}
else {

?>

<input type="text" size="25" name="name" value="<? echo $_POST['name']; ?>">
<?php
}

?>


</td>
</tr>
<tr>
<td align="right">
Email:
</td><td align="left">
<?
if ($_COOKIE['email']) {
   echo $_COOKIE['email'];
}
else {
?>
<input type="text" size="25" name="email" value="<? echo $_POST['email']; ?>">
<?php
}

?>

</td>
</tr>




<tr>
<td colspan="2">
<input type="checkbox" name="terms" checked="checked">I accept terms and conditions.<br/>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="ok" value="SUBMIT" />
</td></tr>
</table>
</form>
<?php
}
?>

 

i can't get it working. Please help!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.