• sammiefields2512gmailcom

    (@sammiefields2512gmailcom)


    Hi

    I put the following code into my functions.php file. Here’s what I want:

    1. I want the cookie to be set only when the visitor goes to Post 1.
    2. When someone visits Post 2, if they have the cookie, they get redirected to Post 3. If they don’t have the cookie, nothing happens.

    Now it seems to be working, but the problem is sometimes, when the cookie isn’t set, I get redirected from the home page :s. I have no idea why.

    I should mention I know nothing about coding. The code below is stuff I got online, mixed and matched it, and did some trial and error with my blog.

    function set_newuser_cookie() {
    
    $currentURL = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    
    //THE ABOVE GETS THE CURRENT URL
    
    	if (!isset($_COOKIE['subscriber']) && $currentURL == 'https://mysite.com/post1') {
    		setcookie('subscriber', no, 0, COOKIEPATH, COOKIE_DOMAIN, false);
    
    //THE ABOVE SETS A COOKIE IF IT FINDS THE CURRENT URL IS /POST1
    
    	}
    }
    add_action( 'init', 'set_newuser_cookie');
    
    //I DON'T KNOW WHAT THE ABOVE IS, BUT I HEAR IT'S IMPORTANT
    
    $currentURL = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    
    //THE ABOVE GETS THE URL AGAIN
    
        if (  isset( $_COOKIE["subscriber"] ) &&  $currentURL == 'https://mysite.com/post2' ) :
            header( "Location: https://mysite.com/post3" );
        endif;
    
    //THE ABOVE REDIRECTS THE VISITOR TO POST3 ON 2 CONDITIONS: 1. THAT THE COOKIE IS SET, 2. THAT THE CURRENT URL IS /POST2
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Hello again. Some good progress there!

    The reason for the add_action() call is so your code executes consistently on each request. The second part of your code gets executed when functions.php loads, which is variable depending on the situation. You should enclose both parts of your code inside the action ‘init’ callback function set_newuser_cookie(). Then they are both executed consistently. Since they are mutually exclusive, they will work properly regardless of execution order within the function.

    What will happen now is if the cookie is not set, the original request will be fulfilled. If it was for the home page, it will go there. If it were for something else, it will go to something else. I see nothing here that would cause the home page to load if the request were for something else and the cookie is not set. If this still happens when both parts are part of the ‘init’ action, something else is at play, something in your theme or one of your plugins.

    Thread Starter sammiefields2512gmailcom

    (@sammiefields2512gmailcom)

    Hi bcworkz. Thanks for replying.

    I got some advice on a different forum, the code is below. It’s cleaner in that you don’t have to request the current post/page url separately, and more word-press orientated. Also, it lets me identify multiple pages more easily.

    You’re right about the whole homepage redirect thing. It was a problem with an app called jitouch on my mac. Now solved :p.

    Again, thanks for replying I really appreciate it.

    In case you’re curious, here’s the code. Do let me know if you think there’s anything wrong with it, or if you think it could be improved.

    function set_newuser_cookie() {
       global $post;
       if (!isset($post->ID)) return;
    
     if ($post->ID == 145 || $post->ID == 1151 || $post->ID == 5524 || $post->ID == 7765) 
    
     {
         if (!isset($_COOKIE['subscriber'])) {
            setcookie('subscriber', no, 0, COOKIEPATH, COOKIE_DOMAIN, false);
         }
       }
    
       if (isset($_COOKIE['subscriber']) && $post->ID == 444 || $post->ID == 913) {
         wp_safe_redirect('/page3');
         exit;
       }
    
    }
    add_filter('template_redirect','set_newuser_cookie',1);
    Moderator bcworkz

    (@bcworkz)

    Glad everything is working well for you, your code looks great!

    My only comment is that a list of 4 or 5 conditions OR’d (the || notation) together in an if() conditional is about the limit of readability. If you are only checking for equality or non-quality against a list of values, consider using in_array() instead. For example, this is equivalent to your second if() line:

    $ids = array(145,1151,5524,7765);
    if(in_array($post->ID, $ids)){//code continues...

    You can see how this form could be easier to maintain as well. I’m not sure but I think this form is possibly more efficient as well. For 4 values, it’s not really worth changing existing code, unless you just feel like it. Just wanted you to be aware in case the ID list should grow, or for any other similar situations. Cheers!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘WordPress Cookie acting weird’ is closed to new replies.