On Mon, 2009-08-03 at 17:35 +0200, Grega Leskovsek wrote:
> Here is one of my final objectives at PHP introductory course ot
> O'Reilly. The strange thing that setting a cookie in that program
> les13uadownload.php doesn't work. I don't have a clue - sometimes in
> other programs when I run them they set cookies and sometimes not. To
> see the full functionality of this "program" I had to set cookie
> sevendays in another program. Please check where is my bug. Thanks in
> advance, Grega from Slovenia
> 
> <?php
> if (!empty($_GET['delete_cookie'])) {
>    setcookie("sevendays", "", time()-3600); //delete the cookie to
> allow to download the file more than 1x in 7days as another user
> }
> 
> if 
> (isset($_POST['check'])AND(isset($_POST['email']))AND(empty($_COOKIE["sevendays"])))
> {#user clicked form download button
>   $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);
>   }
> }#isset(check)
> ?>
> <script type="text/javascript">
>       function setcolor(backgcolor,color) {
>               document.getElementById("file").style.color=color;
>               document.getElementById("file").style.background=backgcolor;
>       }
> 
>   function setStyle(x) {
>       document.getElementById(x).style.background="yellow";
>   }
>   function unSetStyle(x) {
>       document.getElementById(x).style.background="white";
>   }
> 
>             function isFormValid (formToCheck) {
>                 retVal = true;
>               
>                 if (
> (formToCheck.email.value).search(/^[\w!#$%&\'*+\/=?^`{|}~.-]+@(?:[a-z\d][a-z\d-]*(?:\.[a-z\d][a-z\d-]*)?)+\.(?:[a-z][a-z\d-]+)$/i)
> == -1 )
>                 {
>                     element = document.getElementById ("new1Label");
>                     element.style.color = "red";
>                     element.style.fontWeight = "bold";
>                     retVal = false;
>                     retEmVal = false;
>                 }
>                 else
>                 {
>                     element = document.getElementById ("new1Label");
>                     element.style.color = "black";
>                     element.style.fontWeight = "normal";
>                     retEmVal = true;
> 
>                 }
>       
>                 if (formToCheck.downloaded.value=='no') {
>                     ret7daysVal = true;
>                 }else {
>                     ret7daysVal = false;
>                 }
> 
>                 if (!retEmVal)
>                 {
>                     alert ("Please enter a valid e-mail address!");
>                     retVal = false;
>                 }else if (!ret7daysVal)
>                 {
>                     alert ("You can download the file only once in
> seven days!");
>                     retVal = false;
>                 }
> 
>                 return retVal;
> 
>             }
> </script>
> <?php
> require($_SERVER['DOCUMENT_ROOT']."/template_top.inc");
> $ua = $_SERVER['HTTP_USER_AGENT']."<br />";
> if (preg_match("/Macintosh/",$ua)) {
>   if (!preg_match("/Firefox/",$ua)) {
>     echo "For Macintosh You need to use Firefox! <a
> href=\"http://www.apple.com/downloads/macosx/internet_utilities/mozillafirefox.html\";>Download
> here ...</a>";
>     exit();
>   }
> }
> if (preg_match("/Windows/",$ua)) {
>   if (!preg_match("/MSIE/",$ua)) {
>     echo "For Windows You need to use Internet Explorer!";
>     exit();
>   }
> }
> $ip = $_SERVER['REMOTE_ADDR'];
> $ipCheck = substr($ip,0,3);
> if ($ipCheck == "202") {echo "I don't trust You."; exit();}
> ?>
> <?php
> if (!empty($_COOKIE["sevendays"])) echo "IN SEVEN
> ".$_COOKIE["sevendays"]." DAYS!!!";
> if ($_COOKIE["sevendays"]) {
>   echo $_COOKIE["sevendays"];
>   ?>
>   <a href="les13uadownload.php?delete_cookie=1">Not my email: <? echo
> $_COOKIE["sevendays"]; ?>?</a>
>   <?php
> }else {
>   ?>
>   <form method="post" name="vnosnaForma" action="les12uadownload.php">
>   <span id="new1Label">Enter Your e-mail: </span><input type="text"
> size="25" name="email" value="<? echo $_COOKIE["sevendays"]; ?>">
>   <input type="button" name="button" value="Download now!" onClick="if
> (isFormValid(document.vnosnaForma)) {document.vnosnaForma.submit();}"
> />
>   <input type="hidden" name="check" value="1" />
>   <input type="text" name="downloaded" value="<?php if
> (!empty($_COOKIE["sevendays"])) echo $_COOKIE["sevendays"]; else echo
> "no"; ?>"/>
>   </form>
>   <?php
> }
> require($_SERVER['DOCUMENT_ROOT']."/template_bottom.inc");
> print_r($_COOKIE);
> echo "XX".$_COOKIE["sevendays"]."XX";
> ?>
> 
> 
> -- 
> When the sun rises I receive and when it sets I forgive ->
> http://users.skavt.net/~gleskovs/
> All the Love, Grega Leskov'sek
> 
Just a quick thought. You're not trying to read in the value of the
cookie in the same page as you are setting it are you? You can do this
with session variables, but not cookies, as PHP only reads them if they
are sent in with the page request headers. So if you set a cookie, it
won't be visible to your script until the page is refreshed, which
forces the browser to resend the cookies, including the newly set cookie
value.


Thanks,
Ash
http://www.ashleysheridan.co.uk


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to