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.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.
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?
Najam UddinCommented:
// This check must be at the top of every page, e.g. through an include
if(!isset($_COOKIE['termAgree']) || $_COOKIE['termAgree'] != "true"){
    $_SESSION['termAgree_ref'] = $_SERVER['REQUEST_URI'];

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


Open in new window

CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

Julian HansenCommented:
You can do this all client side with javascript
<style type="text/css">
.overlay {
  width: 100%;
  min-height: 100%;
  background: rgba(0,0,0,0.8);
  position: absolute;
  left: 0;
  top: 0;
#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;

Open in new window

    <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>

Open in new window

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') {
      $('#close-dialog').data('target', $(this).attr('href'));
  $('#accepted').change(function() {
    $('#close-dialog').prop('disabled', !$(this).is(':checked'));
  $('#close-dialog').click(function() {
    window.location = $(this).data('target');

Open in new window

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

Also added a close button to the dialog.

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

From novice to tech pro — start learning today.