Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2009-11-24 14:26:17

THE BLUE DRAGON
Member
From: Israel
Registered: 2007-11-16
Posts: 637
Website

first time site visit cookie

Hey
how can I create a cookie on first time site visit?
I want to call a JS function name “startplayer”,
but only on the first time you visit the website.
and I will put that cookie in every page head tag.
(or if you know JS and can please help me here)

Last edited by THE BLUE DRAGON (2009-11-29 18:27:58)

Offline

#2 2009-11-24 15:15:01

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,306

Re: first time site visit cookie

re #1: chs_cookie looks promising.


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

#3 2009-11-24 17:01:31

mrdale
Member
From: Walla Walla
Registered: 2004-11-19
Posts: 2,215
Website

Re: first time site visit cookie

Please post back your solution to #1 I’m gonna do this for a client.

Offline

#4 2009-11-24 19:34:03

THE BLUE DRAGON
Member
From: Israel
Registered: 2007-11-16
Posts: 637
Website

Re: first time site visit cookie

uli wrote:

re #1: chs_cookie looks promising.

I tried, I really did, but I just don’t understand how to work with this plugin.
can you please guide me?

all I need is one time use cookie
if the cookie not exist in the visitor computer do that (call a function),
and if it does, so do nothing.

so I will take this cookie and add it to the head tag of every page,
and create the cookie on the body tag with ‘onunload’.

Last edited by THE BLUE DRAGON (2009-11-24 19:34:36)

Offline

#5 2009-11-24 20:59:10

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,306

Re: first time site visit cookie

Gil, it was a recommendation, I never worked with it. Like I said: It looks promising.

But in the meantime I had an opportunity to play a little with its tags.

In the head portion EDIT 2: As the very first entry of my page I put:

<txp:chs_set_cookie cookie="THE_BLUE_DRAGON" value="TheBlueDragon-Cookie_is_set" />

In the body I put:

<txp:chs_cookie_default cookie="THE_BLUE_DRAGON">
	TheBlueDragon-Cookie does not exist.
 </txp:chs_cookie_default>

<txp:chs_cookie_exists cookie="THE_BLUE_DRAGON">
	TheBlueDragon-Cookie is set.
 </txp:chs_cookie_exists>

… which is kinda like an if/else test, the else tag doesn’t work here. The thing throws errors at you like pebbles, but the “Live status shelter” is pebble-tight, and chs_cookie seems to work. Umm, if there weren’t this cacheing issue, I presume. At least if I call different pages or cmd-shift-refresh the page (Mac) it works relyably.

EDIT: Corrected a fault in the code, the correct code didn’t reduce errors, though.

Last edited by uli (2009-11-24 21:59:25)


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

#6 2009-11-24 23:35:44

THE BLUE DRAGON
Member
From: Israel
Registered: 2007-11-16
Posts: 637
Website

Re: first time site visit cookie

That is very nice!
Thank you Uli !

working good,
except that when you close the browser and go into the site again the cookie is getting lost and it call my function again every time you visit the site.
if you on the site and just moving between pages then it works fine and call my function just once on the first visit.
but when you close the browser and open it again and go to the site then it will start from the begining and call my function once again.

Offline

#7 2009-11-24 23:58:09

MattD
Plugin Author
From: Monterey, California
Registered: 2008-03-21
Posts: 1,254
Website

Re: first time site visit cookie

chs_cookie is using setcookie and is not including a value for the expire parameter. This means that the “cookie will expire at the end of the session (when the browser closes).”

You can change this line

setcookie($thevar, $atts['value']);

to

setcookie($thevar, $atts['value'],time()+3600*24*365);

to set the cookie to expire in 1 year

Last edited by MattD (2009-11-25 00:11:35)


My Plugins

Piwik Dashboard, Google Analytics Dashboard, Minibar, Article Image Colorpicker, Admin Datepicker, Admin Google Map, Admin Colorpicker

Offline

#8 2009-11-25 00:14:23

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,306

Re: first time site visit cookie

Gil, you can use <txp:php>setcookie('THE_BLUE_DRAGON', 'TheBlueDragon-Cookie_is_set', time()+3600*24);</txp:php> instead of the txp:chs_set_cookie line.

Edit: As usual, Matt was faster :)

Last edited by uli (2009-11-25 00:15:58)


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

#9 2009-11-25 00:25:30

THE BLUE DRAGON
Member
From: Israel
Registered: 2007-11-16
Posts: 637
Website

Re: first time site visit cookie

Thanks to both of you!
now the cookie got a lot of better flavor that stays much longer ;)

Offline

Board footer

Powered by FluxBB