PHP Cookies Accept terms

I have 4 pages on my site that I want to lock down and have a visitor Accept terms before they view.

I have a menu choice with 4 links.  When a person clicks on any of these 4 links, for the first time they have to agree to the terms of service.  After the first time, they don't have to agree again.

I do this with a cookie?  I'm not certain on how to proceed.
LVL 7
rgranlundAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Najam UddinCommented:
Cookie is okay if you don't have login capability in web. However cookies can be cleared, so If a user clear his/her cookie and comes back to your web site,  they will see "agree terms" again. If you have login capability you can store in db which users have accepted terms.
0
rgranlundAuthor Commented:
I will need to use cookies.  How do I setup which pages need the accept terms?  Can I open the accept
terms in a lightbox?
0
Najam UddinCommented:
<?php
// This check must be at the top of every page, e.g. through an include
session_start();
if(!isset($_COOKIE['termAgree']) || $_COOKIE['termAgree'] != "true"){
    $_SESSION['termAgree_ref'] = $_SERVER['REQUEST_URI'];
    header("Location: http://your.site/term.php");
    die();
}
?>

<?php
// You need to set the cookie in term.php
session_start();
setcookie("termAgree", "true", time()+60*60*24*90); // Remember answer for 90 days
    if(!isset($_SESSION['termAgree_ref'])) {
        $_SESSION['agecheck_ref'] = "/";
    }
 header("Location: http://your.site" . $_SESSION['termAgree_ref']);

?>

Open in new window

0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

Julian HansenCommented:
You can do this all client side with javascript
CSS
<style type="text/css">
.overlay {
  width: 100%;
  min-height: 100%;
  background: rgba(0,0,0,0.8);
  position: absolute;
  left: 0;
  top: 0;
  z-index:10000;
}
#accept-terms {
  background: white;
  border: 1px solid #428bca;
  display: none;
  left: 37.5%;
  padding: 20px;
  position: absolute;
  top: 12.5%
  width: 25%;
  z-index: 20000;
}
</style>

Open in new window

HTML
    <a href="t1210-restricted.html" class="restricted">Restricted Page</a>
    <div id="accept-terms">
      <h2>Terms of use</h2>
      <p>Before you can proceed you must accept the terms and conditions of this site</p>
      <p> <input type="checkbox" id="accepted"/> I have read the <a href="t1210-terms.html" target="_blank">terms and conditions</a></p>
      <p class="text-right"><button class="btn btn-primary" id="close-dialog" disabled>Continue</button></p>
    </div>

Open in new window

JavaScript
<script>
function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i=0; i<ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1);
        if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
    }
    return "";
} 

function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = "expires="+d.toUTCString();
    document.cookie = cname + "=" + cvalue + "; " + expires;
} 

$(function() {
  $('a.restricted').click(function(e) {
    var accepted = getCookie('terms');
    if (accepted != 'accepted') {
      e.preventDefault();
      $('body').append(
        $('<div/>')
        .addClass('overlay')
        .height(
          $(document).height()
        )
      );
      $('#close-dialog').data('target', $(this).attr('href'));
      $('#accept-terms').fadeIn();
    }
  });
  
  $('#accepted').change(function() {
    $('#close-dialog').prop('disabled', !$(this).is(':checked'));
  });
  $('#close-dialog').click(function() {
    setCookie('terms','accepted');
    window.location = $(this).data('target');
  });
});
</script>

Open in new window

Working sample here
1
rgranlundAuthor Commented:
@Julian, so any link with the class restricted will get that accept terms?
0
Julian HansenCommented:
Yes - I have updated the sample to include 2 restricted links and one unrestricted.

Also added a close button to the dialog.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.