1

My site needs a mechanism where a user is sent to agreement page when he visits the site for the first time. Once he accepts the agreement, it is saved in cookie. He can browse any page directly after that.

I am using javascript for cookie and it was working fine as long as the permalink setting was default one(http://mydomain.com/?p=123).

As soon as I made change in permalink and made it postname (http://mydomain.com/sample-post/) it started showing a problem.

(The change in .htaccess file was done as recommended by wordpress)

The problem is: the user is again and again redirected to the agreement page even if he clicks on agreement button.

Following is the code.

In header.php

        setCookie("requested_url", document.URL, 4);
        var agreement = getCookie("agreement");

        if(agreement == "yes"){
            var requested_url = getCookie("requested_url");
        }

        else{
            window.location.assign("http://mydomain.com/?page_id=22");
        }

In header.php, it checks if the cookie named agreement is set to yes. If yes then do nothing. If not set to yes then redirect to agreement page(/?page_id=22). The agreement(cookie) is set in this page.

In page_id=22(page-22.php)

       <script type = "text/javascript">

            function yesbutton()
                {
                    setCookie("agreement", "yes", 5);
                    window.location.assign(getCookie("requested_url"));
                }

                </script>

        <div class="agreementButton">
            <input type = "button" onclick = "yesbutton()" value = "Accept">
        </div>

After digging through the code, I found that in header.php the value of agreement is not being accessed correctly.

In page-22.php setCookie("agreement", "yes", 5); is working fine. I can do an alert and get its value(yes) here.

But when I do alert of the same cookie in header.php it alerts blank space. In header.php this code returns a blank popup.

        var agreement = getCookie("agreement");
        alert(agreement);

This clearly means the cookie(agreement) that is being set to yes in page-22.php is not being accessed in header.php correctly. But why this is happening I could not get any clue. Strange is when the permalink is default one everything works fine. As soon as permalink is set to postname, the cookie(agreement) becomes blank in header.php.

Update: setCookie and getCookie functions that exists in both header.php and page-22.php.

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

        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) != -1) return c.substring(name.length, c.length);
            }
            return "";
        }

These two functions are working fine in both pages.

The problem is:

      setCookie("agreement", "yes", 5); 

    that is done in page-22.php sets cookie correctly.

But when the same cookie is accessed in header.php with

       var agreement = getCookie("agreement");

       the value of agreement is blank.

       alert(agreement) returns blank space.

Any clues/hints are welcome.

  • There is not enough information to answer your question. All your code is javascript, not PHP. Additionally, there is no function named "setCookie" or "getCookie" in javascript, and you don't state what libraries you are using. The answer is most likely that your setCookie function is using the current page path (cookies have a URL path component to them), and thus other pages on different URLs cannot access those cookies, but there's no real way to tell without seeing that code as well. For JS problems, you should try a different Stack, this is not a WordPress specific question. – Otto Oct 30 '14 at 20:34
  • Actually only javascript is involved for redirection and setting cookie. I will update and add setCookie and getCookie function too. Thanks. – Sandesh Yadav Oct 30 '14 at 20:37
  • You are right actually the problem was with the current page path. I changed it and it works now. Thanks a lot. Please write it as an answer so that I can mark it as accepted. Thanks again. – Sandesh Yadav Oct 30 '14 at 21:08
1
+50

From comment:

There is not enough information to answer your question. All your code is javascript, not PHP. Additionally, there is no function named "setCookie" or "getCookie" in javascript, and you don't state what libraries you are using.

The answer is most likely that your setCookie function is using the current page path (cookies have a URL path component to them), and thus other pages on different URLs cannot access those cookies, but there's no real way to tell without seeing that code as well.

For JS problems along these lines, you should try a different Stack, this is not a WordPress specific question, but a general javascript issue.

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.