I am truly a newbie, but I have figured out a few things in a short amount of time. Some helpful hints to get me jump-started would be most welcome.

The user will access a page 'request.php'. It will check for a cookie named 'agree'. If that cookie is not found, it will send them to a page (agreement.php) with a form that requires a user to select "I Agree" and then submit the form. When the form is submitted, I want to set a cookie (expires in a month) that indicates that they have agreed. I know how to set cookies and read them, but I have 2 questions:

  1. What/where do I submit the form to (value="")?
  2. Where do I specify setcookie?

The page with the text below is 'request.php'. If the cookie 'agree' exists, user is sent to the 'logon.php' page. Otherwise, the user is sent to the 'agreement.php' page.

<?php
if(isset($COOKIE['agree'])){
$agree = $
COOKIE['agree'];
include ('logon.php');
}

else

{

include ('agreement.php');

}
?>

The 'logon.php' page will also have some code to check for the cookie, in case a user comes through the back door directly to the logon.php page.

The 'agreement.php' page looks something like this:

<form action="POST" value="">
<tr>
<td>
Please read the requirements for use of this system. We can process your request only if it meets one or more of the following criteria:
<ul>
<li><p> the request is for reason X</p>
<li><p> the request is for reason Y</p>
<li><p> the request is for reason Z</p>
</ul>

     </td>
   </tr>
  <tr>
    <td
        <input name="accept" type="checkbox" value="true"> I agree to the above terms and conditions.

       <input id="submit" type="submit" value="Continue">
   </td>

 </tr>

</form>

Obviously, I mostly need help with knowing how to submit the form. It seems like since there is only 1 field (accept), it would be a matter of reading that variable into a cookie. I am treading water at this point.

Thanks.

    Before anyone catches it, I actually meant:

    <form method="POST" action="">

    Reading a bit more about forms and PHP, I am wondering if this would work for submitting the form:

    <form method="POST" action="<?php echo $PHP_SELF;?>">

    Still, I need to figure out where to set the cookie.

    Could I add something in the same file (agreement.php), something like this:

    if(isset($submit) {

    setcookie ("agree", $agree);

    }

    I would like to be able to validate that the form has been submitted, but that the user also checked "agree".

    I hope this isn't confusing.

      The technique that I use is this:

      if (cookie exists) { include("logon.php"); exit; }
      else {include("agreement.php"); exit; }

      So the two lines above are the entire contents of request.php. If the person has agreed, then they see the logon form. If they haven't agreed, then they see the agreement form.

      Using this technique:
      <form method="POST" action="<?php echo $PHP_SELF;?>">
      can be a little confusing at first. Personally, I like to start people off with two separate files (the form and the action). Then as they become better programmers, then they can experiment with making the form also be the action. I'm afraid that's just going to complicate things for you at this stage.

      Here's where to set the cookie:

      Have a page called registration.php that looks like this:
      <form action="registration2.php">
      <input blah blah blah bunch of input fields>
      </form>

      Then registration2.php looks like this:
      <? check to see if he clicked the "I agree" check box. If so, then set cookie.
      Do whatever other registration things you want like send emails or write to databases
      ?>

      Then you have logon.php that looks like this:

      if (cookie does not exist) { include("agreement.php"); exit;}
      <form action="logon2.php>
      <input username and password and whatnot>
      </form>

      Hopefully that helps. Feel free to ask more detailed questions when you have some code samples to show.

        Thank you so much for your reply. It looks like I am close to being on the right track. I have the request.php working correctly if I manually set or delete the cookie. However, setting the cookie in the form has been more challenging.

        I still am a little confused about the page with the form, in my case the agreement.php file. I am assuming that in my specific case, the registration.php and registration2.php files would be agreement.php and agreement2.php, like the code below. After the cookie is set, I would like it to go to the logon.php page. I currently have the agreement2.php page saying "If the form was submitted and the 'agree' box was checked and if the cookie isn't present, set the cookie and display the logon.php page. If any of those is not true, display the agreement.php page.

        When I check the 'agree' box and submit, I get a blank page.

        agreement.php

        <html>
        <form action="agreement2.php">
        I agree checkbox goes here
        </form>

        agreement2.php

        <?php
        if(isset($POST["submit"]) && isset($POST["agree"]) && !isset($_COOKIE['agree']))
        { setcookie ("agree", $agree);
        include ('logon.php'); }
        else
        { include ('agreement.php') };
        ?>

          You have the general model working correctly. You put the semi-colon outside the { } brackets after the include agreement.php command which is a syntax error and you're seeing that as a blank page since you have error reporting turned off. So before Weedpacket gets a chance to say it, You really should have error reporting turned on during the development phase. 🙂

            Thank you for the input. I have made the change.

            Now when I submit the form, it just remains on the form page and the cookie is not set.

            By the way, how do I turn on error reporting?

              I'm not sure but I think your logic for determining when the user has clicked "agree" is too restrictive - but I don't have your code to test so I'm not sure. Try changing this:
              if(isset($POST["submit"]) && isset($POST["agree"]) && !isset($COOKIE['agree']))
              to this:
              if(isset($
              POST["agree"]) && !isset($_COOKIE['agree']))

              So basically,  you are saying, "If they posted that they agree and the cookie isn't set, then set it and bring up the logon page.
              
              As for turning on error checking,  my sysadmin set that up for me so I don't know the setting myself.  You need to modify the php.ini file.  Search that file for "error" and you'll probably see what needs to be fixed.  Or you could search the PHPbuilder forums or read the FAQ's - that question has been answered many times.

                Okay, I was able to turn on error checking in the php code itself. That helped with some php referrers and stuff like that, which I fixed.

                So, now I send the user to request.php page. If the cookie is not found, it sends the user to the logon.php page. If the cookie is found, it sends the user to the agreement.php page. I was able to test this by setting the cookie manually.

                Here is request.php

                <?php
                if(isset($COOKIE['agree']))
                { $agree = $
                COOKIE['agree'];
                include ('logon.php');
                exit;
                }
                else
                { include ('agreement.php');
                exit;
                }
                ?>

                So, assuming the cookie was not set, the user is sent to agreement.php. The checkbox for 'agree' is checked and submitted. There is no error reported, but the resulting page is blank. If I check, the cookie has been set. It isn't until I reload the page that it sends me to the logon.php page. I know there is something about cookies that require going to another page before the cookie takes affect, or something like that.

                Here is agreement2.php

                <?php
                error_reporting(E_ALL);
                if(isset($POST["agree"]) && !isset($COOKIE['agree']))
                { setcookie ("agree", "agree"); }
                else
                { include ('logon.php'); }
                ?>

                If I change the above to this, when submitted, it will show the text 'The cookie has been set' and the cookie has been set.

                <?php
                error_reporting(E_ALL);
                if(isset($POST["agree"]) && !isset($COOKIE['agree']))
                { setcookie ("agree", "agree"); }
                else
                echo 'The cookie has been set';
                ?>

                  Oh. Look at the code you have for agreement2.php :

                  <?php
                  error_reporting(E_ALL);
                  if(isset($POST["agree"]) && !isset($COOKIE['agree']))
                  { setcookie ("agree", "agree"); }
                  else
                  { include ('logon.php'); }
                  ?>

                  Notice what happens after you set the cookie... nothing! Your code does this: If cookie has no value, then set the cookie and don't do anything else. I think you really want the logic to be like this:

                  • If cookie is not set and they DID click the agree button, then set the cookie AND show them the logon page
                  • If cookie is not set and they DID NOT click the agree button, then show them the agreement form
                  • If cookie IS set, then show them the logon page.

                  Which you might code like this:

                  <?php
                  error_reporting(E_ALL);
                  if(isset($POST["agree"]) && !isset($COOKIE['agree']))
                  { setcookie ("agree", "agree"); include("logon.php"); }
                  if(!isset($POST["agree"]) && !isset($COOKIE['agree']))
                  { include ('agreement.php'); }
                  if (isset($_COOKIE['agree']))
                  { include("logon.php"); }
                  ?>

                    Okay, that makes sense. I guess I didn't realize how many conditionals were needed.

                    After implementing your recommended changes, the cookie is set, but the form submission page has to be refreshed before the logon page is displayed, just like before, except now there is no blank page.

                    I tried moving the check for the cookie to the top and also tried removing the && !isset($_COOKIE['agree'])) in line 4, but still it doesn't go to the logon page until I refresh.

                    <?php
                    if (isset($COOKIE['agree']))
                    { include("logon.php"); }
                    if (isset($
                    POST["agree"])
                    { setcookie ("agree", "agree"); include("logon.php"); }
                    if (!isset($POST["agree"]) && !isset($COOKIE['agree']))
                    { include ('agreement.php'); }
                    ?>

                    I also tried changing the 2nd and 3rd if statements to elseif, like this, but still the same result.

                    <?php
                    if(isset($COOKIE['agree']))
                    { include("logon.php"); }
                    elseif(isset($
                    POST["agree"]) && !isset($COOKIE['agree']))
                    { setcookie ("agree", "agree"); include("logon.php"); }
                    //else(!isset($
                    POST["agree"]) && !isset($_COOKIE['agree']))
                    else
                    { include ("agreement.php"); }

                      What does logon.php have in it? Just follow the logic.

                        Well, that may be the problem, though I don't understand exactly why.

                        The first part of logon.php looks like this:

                        <?php
                        if(!isset($_COOKIE['agree'])) {
                        include ('agreement.php');
                        exit;
                        }
                        ?>

                        That code was intended to look for the cookie if the user went directly to the logon.php page and if it didn't exist, send them to the agreement.php page. However, it appears to be interfering somehow, maybe because the form has already checked for the cookie?

                          Ok. That's the problem.

                          When you set a cookie with the setcookie command, the only thing you are doing is setting the cookie on the user's machine. PHP doesn't immediately reset the $_COOKIE variable,

                          Or put another way, the only time the $COOKIE variable gets automatically populated by PHP is when the page is loaded. Therefore, if you say: setcookie("foo","bar"); echo $COOKIE['foo']; The result will be nothing.

                          So you are, in fact, setting the cookie on the user's side but $_COOKIE isn't getting it's value until you reload the page.

                          Therefore, a simple solution would be to follow every setcookie command with something like this:
                          $COOKIE['agree'] = $agree;
                          Another solution would be (in logon.php) to check for the existance of a cookie OR for $
                          POST['agree']
                          Either technique will work.

                          What's happenning is that you are setting the cookie and including logon.php and checking $_COOKIE which doesn't have it's value yet.

                          That should do it.

                            Write a Reply...