Jump to content

Setting a php cookie and redirect in Wordpress


sammiefields2512

Recommended Posts

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 to Page 3 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 = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];


//THE ABOVE GETS THE CURRENT URL


if (!isset($_COOKIE['subscriber']) && $currentURL == 'http://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 = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];


//THE ABOVE GETS THE URL AGAIN


    if (  isset( $_COOKIE["subscriber"] ) &&  $currentURL == 'http://mysite.com/post2' ) :
        header( "Location: http://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

 

Please help!

Link to comment
Share on other sites

I don't see anything wrong with the code. When anything dealing with cookies happens "sometimes," you are probably looking at caching issues. I'm guessing either the cookie is not in the state you think it is, or Wordpress is caching a page or variable.

Link to comment
Share on other sites

Hi Lemmin, thanks a lot for replying.

 

I think you're right, there doesn't seem to be any problems with the code. It might rather have had something to do with another piece of software on my mac (jitouch) interfering with the redirect. It seems to be alright.

 

I've since cleaned up the code with some help from a guy on the wordpress support forums. Here's what I'm using now, and it seems to be fine:

 

 

function set_newuser_cookie() {
   global $post;
   if (!isset($post->ID)) return;


 if ($post->ID == 1) 


 {
     if (!isset($_COOKIE['subscriber'])) {
        setcookie('subscriber', no, 0, COOKIEPATH, COOKIE_DOMAIN, false);
     }          
   }


   if ($post->ID == 2 && isset($_COOKIE['subscriber'])) {
     wp_safe_redirect('/page3');
     exit;
   }




}
add_filter('template_redirect','set_newuser_cookie',1);
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.