3

When users visit a page on my site, I've written code to set a value:

    if(window.location.href.indexOf("promotionpage") > -1) {
    Cookies.set('landingpage', 'kent', { expires: 30, path: '/promotionpage' });
}

This is working great and the cookie is being set. Depending on if the above cookie is set, I want to then add a module position to the index.php page, I've written the following to read if the cookie is set and then include a module position, but it's not working:

        <?php
        if(isset($_COOKIE['kent'])){ ?>
            <jdoc:include type="modules" name="kentModal" style="none" />
    <?php } ?>
  • are you trying to show the module on the same page as where you set the cookie or on subsequent pages – jdog Sep 16 '15 at 23:28
  • I'm trying to load it on subsequent pages once the cookie has been set. – RustyJoomla Sep 17 '15 at 5:49
2

You set the cookie via JS with name: landingpage BUT you checking in PHP for a cookie name: kent.

Just fix that ;)

Your Cookie-JS-Plugin works like:

Cookies.set('name', 'value');
1

The way you use cookies seems wrong, unless there is a library that you use here.

According to quirksmode you would use something like

if(window.location.href.indexOf("promotionpage") > -1) {
     document.cookie = 'landingpage=kent; expires=<?php echo date('D, j M Y H:i:s', time()+30);?> path=/promotionpage');
}

This question has a good list of JavaScript libraries to manage cookies easier https://stackoverflow.com/q/4825683/123594

A couple of other comments:

  • You are checking for "promotionpage" to be anywhere in the url, but the cookie is only valid if your path BEGINS WITH "promotionpage"
  • Do you want the cookie really only to be valid for 30 seconds?
  • Thanks for your answer, but the cookie is being set correctly already I just can't work out how to read it and display the module position. For your info, I'm using js.cookie.js from Github here github.com/js-cookie/js-cookie – RustyJoomla Sep 17 '15 at 8:24
  • Maybe its expired after 30 seconds already? Ignore, I see its in days – jdog Sep 17 '15 at 8:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.