开发者

How to set a cookie after the user has agreed to the terms and conditions form?

My question is, how do I set a cookie after the user has agreed to the Terms and Conditions page? I was going to add this code

<?php //Calculate 30 days in the future    
//seconds * minutes * hours * days + current time    
$inOneMonth = 60 * 60 * 24 * 30 + time(); 
setcookie('lastVisit', date("G:i - m/d/y"), $inOneMonth); 
?>

To the below code, but depending on where I put it I either break the javascript or on page refresh the cookie is set (even though the terms and conditions have not been agreed to).

<?php
// Get the user's ultimate destination
$dest = $_GET['dest'];
// Show the terms and conditions page
//check for cookie
if(isset($_COOKIE['lastVisit']))
    $visit = $_COOKIE['lastVisit']; /* Add redirect later   document.location.href='http://<?php echo $dest; ?>';   add redirect code later*/
else
    echo "No cookies present-remove this msg later";
?>

<HTML>
  <HEAD>
    <TITLE>User Agreement</TITLE>
    <script language="javascript">

function valbutton(thisform) {
// validate myradiobuttons
  myOption = -1;
  for (i=thisform.myradiobutton.length-1; i > -1; i--) {
    if (thisform.myradiobutton[i].checked) {
      myOption = i;
    }
  }
  if (myOption == -1) {
   alert("You must choose either YES or NO");
   return false;
  }
  if (myOption == 0) {
   alert("You must agree to the Terms and Conditions to download");
   return false;
  }
  thisform.submit(); // this line submits the form after validation
}
</script>

  </HEAD>
  <BODY>
    <H1> Terms and Conditions </H1>
    <P>Before downloading you mu开发者_StackOverflow中文版st agree to be bound by masking tape</P>
<form name="myform" action="http://<?php echo $dest; ?>"> 
<input type="radio" value="1st value" name="myradiobutton" />NO<br />
<input type="radio" value="2nd value" name="myradiobutton" />YES<br />
<input type="submit" name="submitit" onclick="valbutton(myform);return false;" value="ANSWER" />
</form>

  </BODY>
</HTML>

I'm sure this is an easy question, but I really don't know what I'm doing. Any tips are much appreciated.


and one more thing... look at your setcookie call:

setcookie('lastVisit', date("G:i - m/d/y"), $inOneMonth); 

if think the right way is

setcookie('lastVisit', date("G:i - m/d/y", $inOneMonth)); 


A better approach to do this would be to evaluate/validate the input also on the server side. Normally you NEED to do this anyways because of security reasons, Javascript validation is only for user friendliness, not for security! In your case there is no real user input and therefore no direct threat.

Validate the input as you usually do with PHP by accessing the $_POST array and validating different form fields as required. When the whole chain of validations is successful, set the cookie!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜