funkgut Posted May 8, 2009 Share Posted May 8, 2009 I am having an issue with cookies in my php. Here is the scenario. I have a form that when you access it you are entering as a guest. When you fill out the form and submit you are shown the date submitted and all the info you typed in. Now, when you go back to the form page, it recognizes the cookie and tells you cannot send another email in a 24 hours period. All this works fine. What I can't figure out is how to tell the user what time they submitted the original email on. I assume it can be retrieved form the cookie, but for the life of me I can't figure out how. Here is the form page contact.php <?php setcookie("assign", "ProfClower", time()+86400); ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Messer Consulting Services .: Contact Us :.</title> <link rel="STYLESHEET" type="text/css" href="styles.css"> <style type="text/css"> <!-- td.form { color: #ffffff; font-family: "Verdana","Arial"; font-size: 11; } td.main { color: #000000; font-family: "Verdana","Arial"; font-size: 12; } font.form_check { color: red; } input { font-family: "Verdana","Arial"; color:#606060; font-size: 11px; } textarea { font-family: "Verdana","Arial"; color:#606060; font-size: 11px; } div#form_box { margin: 2px; width: 651px; border: 1px; border-style: solid; border-color: #606060; background: #64798B; padding: 5px; } --> </style> </head> <body> <div align="center"> <div class="contentBorder"> <div class="mainContent"> <br> <table width="651" cellpadding="2" cellspacing="3" border="0"> <tr> </td> <?php if (isset($_COOKIE["assign"])) echo "Welcome " . $_COOKIE["assign"] . "!<br />"; else echo "Welcome guest!<br />"; ?> </td> </tr> <tr> <td class="bodyText">We would love to hear from you! Please feel free to fill out the form below with any questions or comments you may have</td></tr> <tr> <td class="bodyText">Please fill out and submit the form on this page to contact us. We will get back to you as soon as we can. Note that fields marked with (<font class="form_check">*</font>) are required fields. </td> </tr> <tr> <td class="bodyText"> <form method="post" action="sendeail.php"> <?php $ipi = getenv("REMOTE_ADDR"); $httprefi = getenv ("HTTP_REFERER"); $httpagenti = getenv ("HTTP_USER_AGENT"); $todayis = date("l, F j, Y, g:i a") ; ?> <input type="hidden" name="ip" value="<?php echo $ipi ?>" /> <input type="hidden" name="httpref" value="<?php echo $httprefi ?>" /> <input type="hidden" name="httpagent" value="<?php echo $httpagenti ?>" /> <input type="hidden" name="todayis" value="<?php echo $todayis ?>" /> Your Name: <br /> <input type="text" name="visitor" size="35" /> <br /><br /> Your Email:<br /> <input type="text" name="visitormail" size="35" /> <br /><br /> Mail Message: <br /> <textarea name="notes" rows="4" cols="40"></textarea> <br /><br /> <?php if (isset($_COOKIE["assign"])) echo "You sent an email on ". $_COOKIE["todayis"] ." with the subject Contact Inquiry. You may not send more than one email in any 24 hour period."; else echo "<input type=submit value=Send Mail>"; ?> </form> </td> </tr> </table> </div> </div> </div> </body> </html> and here is the action page. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Sendemail Script</title> </head> <body> <?php $ip = $_POST['ip']; $httpref = $_POST['httpref']; $httpagent = $_POST['httpagent']; $visitor = $_POST['visitor']; $visitormail = $_POST['visitormail']; $notes = $_POST['notes']; if (eregi('http:', $notes)) { die ("Do NOT try that! ! "); } if(!$visitormail == "" && (!strstr($visitormail,"@") || !strstr($visitormail,"."))) { echo "<h2>Use Back - Enter valid e-mail</h2>\n"; $badinput = "<h2>Feedback was NOT submitted</h2>\n"; echo $badinput; die ("Go back! ! "); } if(empty($visitor) || empty($visitormail) || empty($notes )) { echo "<h2>Use Back - fill in all fields</h2>\n"; die ("Use back! ! "); } $todayis = date("l, F j, Y, g:i a") ; $subject = "Contact Inquiry"; $notes = stripcslashes($notes); $message = " $todayis [EST] \n Message: $notes \n From: $visitor ($visitormail)\n Additional Info : IP = $ip \n Browser Info: $httpagent \n Referral : $httpref \n "; $from = "From: $visitormail\r\n"; mail("nofx@gti.net", $subject, $message, $from); ?> <p align="center"> Date: <?php echo $todayis ?> <br /> Thank You : <?php echo $visitor ?> ( <?php echo $visitormail ?> ) <br /> Message:<br /> <?php $notesout = str_replace("\r", "<br/>", $notes); echo $notesout; ?> <br /> <?php echo $ip ?> </p> </body> </html> Any help would be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/157356-retrieving-cookie-values/ Share on other sites More sharing options...
w3evolutions Posted May 8, 2009 Share Posted May 8, 2009 just set another cookie with: setcookie("origTime", microtime(true), time()+86400); Quote Link to comment https://forums.phpfreaks.com/topic/157356-retrieving-cookie-values/#findComment-829397 Share on other sites More sharing options...
Zhadus Posted May 8, 2009 Share Posted May 8, 2009 I'd recommend going about it differently. Anyone can disable cookies and then send multiple emails. Why don't you store the information in a database. You can track the user by their email or by their IP address (The latter being less useful to you). From that you can just use a timestamp for when the email was sent. Quote Link to comment https://forums.phpfreaks.com/topic/157356-retrieving-cookie-values/#findComment-829401 Share on other sites More sharing options...
funkgut Posted May 8, 2009 Author Share Posted May 8, 2009 Thanks for the quick replies W3, I understand you point just not how to implement. If I set another cookie should I do that in the contact.php or the action page? The second question would be how would I retireve the time value for that cookie......sorry obviously I am a noob....... Zhadus, I agree a DB would be a better implementation but not really an option. Quote Link to comment https://forums.phpfreaks.com/topic/157356-retrieving-cookie-values/#findComment-829406 Share on other sites More sharing options...
Zhadus Posted May 8, 2009 Share Posted May 8, 2009 If DB is not an option, perhaps use a flatfile (textfile) to save the information. Otherwise for your question trying to draw the cookie value, it's: $variable = $_COOKIE['origTime']; Quote Link to comment https://forums.phpfreaks.com/topic/157356-retrieving-cookie-values/#findComment-829418 Share on other sites More sharing options...
premiso Posted May 8, 2009 Share Posted May 8, 2009 Why not look into using sessions you can change the session_set_cookie_params to make it last longer than a browser close. This way all data is housed on the server still and just the sessionid is stored in a cookie on the user's box. A bit more secure than using cookies for everything. Quote Link to comment https://forums.phpfreaks.com/topic/157356-retrieving-cookie-values/#findComment-829425 Share on other sites More sharing options...
funkgut Posted May 8, 2009 Author Share Posted May 8, 2009 last question I promise.....and by the way thanks you guys are great.......how would I format the date and where would I do that using the 'origTime' coookie proposed by W3 Quote Link to comment https://forums.phpfreaks.com/topic/157356-retrieving-cookie-values/#findComment-829736 Share on other sites More sharing options...
premiso Posted May 8, 2009 Share Posted May 8, 2009 Look into the date function. Quote Link to comment https://forums.phpfreaks.com/topic/157356-retrieving-cookie-values/#findComment-829738 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.