Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-25-2013, 05:15 PM   PM User | #1
sammiefields251
New to the CF scene

 
Join Date: Apr 2013
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
sammiefields251 is an unknown quantity at this point
Question Setting a cookie on Wordpress site

Hi all

I hope this is the right place for this question, since form what little I know, cookies are set using javascript.

Here's what I'd like to do on my self-hosted Wordpress website.

1. I'd like to set a cookie when a person visits one particular page on my website, let's call it Page 1.
2. Then, when that visitor goes to another unrelated page (Page 2), he/she gets automatically redirected to Page 3 (because of the cookie that was set when they visited Page 1). Visitors without the cookie, don't ever get to see Page 3.

Is this doable?

Thanks so much.


Sammie
sammiefields251 is offline   Reply With Quote
Old 04-25-2013, 07:19 PM   PM User | #2
JonBMN
New Coder

 
Join Date: Feb 2013
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
JonBMN is an unknown quantity at this point
Try it and post your code!
JonBMN is offline   Reply With Quote
Old 04-26-2013, 02:37 PM   PM User | #3
sammiefields251
New to the CF scene

 
Join Date: Apr 2013
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
sammiefields251 is an unknown quantity at this point
Well OK, after much research and trial and error, here's what I got:


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


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

Thanks!!
sammiefields251 is offline   Reply With Quote
Reply

Bookmarks

Tags
cookie, cookies, redirect, wordpress

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:51 PM.


Advertisement
Log in to turn off these ads.