Avatar of Jon Imms
Jon Imms
Flag for United States of America asked on

Convert PHP session cookie to JS

Hey there,  I have a PHP session cookie on our site which sets when a visitor comes to our site from a paid ad, and follows them through the site, and passes the variable to our CRM when they fill out a form.

See the PHP code below. Works!

function set_ad_cookies() {
	
	$vars = array('wordpress_sourcetagji');
	foreach ($vars as $k) {
		if (isset($_GET[$k]) && !isset($_COOKIE[$k])) {
			setcookie($k, $_GET[$k], 0, '/', $domains['cookie_domain']);
		}
	}
	
	  if (!isset($_COOKIE['wordpress_sourcetagji'])) {
		setcookie('wordpress_sourcetagji', $_GET['wordpress_sourcetagji'], 0, '/', $domains['cookie_domain']);
	  }
  }
  add_action('init', 'set_ad_cookies');

Open in new window


We are now finding that caching is stripping the cookie value when they navigate to a different page. Flywheel have said that this will happen as it's php and need to keep the caching on, and the only solution is to create the cookie in JS.

I've got very basic js skills, so how could i convert the above PHP code into JS so that   Scenario :
User1 comes into the site from an ad  https://mysite.com/?wordpress_sourcetagji=this-monster-cookie
User 1 visits varous pages on our site, and then fills out a form.
Cookie is still active and keeps it's value until the session ends.
PHPJavaScriptContact Management

Avatar of undefined
Last Comment
Zvonko

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Zvonko

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Jon Imms

ASKER
Awesome!! Thank You
Zvonko

You are welcome  :  )

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes