Re: setting cookie doesn't work

From: Date: Mon, 03 Aug 2009 18:44:00 +0000
Subject: Re: setting cookie doesn't work
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-296231@lists.php.net to get a copy of this message
I made two files now of the program: (the main one and the one where I set the cookie and open pdf)and I noticed that it doesn't work setting the cookie the first time I call the file where I set the cookie but only after the second header redirection. How do I refresh the page in php? Thanks in advance, Grega from Slovenia 2009/8/3 Grega Leskovsek <mavricek@gmail.com>: > And here is the objective: > Alter your Acme, Inc. download interface from the previous lesson(I > had to not to allow users on computers straing IP with 202 and allow > only IE for Win and FF for Mac to download file) so 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. > > As always, validate the forms, comment your code, and explain your results! > > > 2009/8/3 Grega Leskovsek <mavricek@gmail.com>: >> 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 > //You dont need this line> > 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 >> } > //You dont need this line> > 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 >> > > > > -- > When the sun rises I receive and when it sets I forgive -> > http://users.skavt.net/~gleskovs/ > All the Love, Grega Leskov'sek > -- When the sun rises I receive and when it sets I forgive -> http://users.skavt.net/~gleskovs/ All the Love, Grega Leskov'sek

« previous php.general (#296231) next »