# Set Cookie from URL Parameter and using it in HTML on a different page.

**URL:** <https://community.hubspot.com/t/set-cookie-from-url-parameter-and-using-it-in-html-on-a-different-page/680>\
**Category:** CMS Development\
**Created:** [January 9, 2017, 7:41pm UTC](https://community.hubspot.com/t/set-cookie-from-url-parameter-and-using-it-in-html-on-a-different-page/680 "2017-01-09T19:41:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![REVEALiO](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/revealio/32/99678_2.png) [@REVEALiO](https://community.hubspot.com/u/REVEALiO)\
**Post date:** [January 9, 2017, 7:41pm UTC](https://community.hubspot.com/t/set-cookie-from-url-parameter-and-using-it-in-html-on-a-different-page/680/1 "2017-01-09T19:41:17Z")

</div>

I am setting up an affiliate link area for my site and need to be able to store a URL query string into a cookie that expires in a 7 days and pull it out into HTML later down the road.

Users will click on a link like: [http://examplesite.com?afil=randomvalue](http://examplesite.com/?afil=randomvalue).

I want to pull the key ‘afil’ and the value ‘randomvalue’ and put it in a cookie in the browser.

This cookie will be used later (on another page) in a link from one of [examplesite.com](http://examplesite.com) to [order.examplesite.com](http://order.examplesite.com) but I can’t keep the parameter (query string) in the URL.

I can do this on another site using PHP but HubSpot doesn’t let me use PHP. I saw other posts mentioning the use of HUBL.

My PHP code is:

\<body\>

\<?php

$value=$\_GET[‘afil’]; //the value from the url

setcookie(“afilCookie”, $value, strtotime( ‘+7 days’ )); /\* expire in 7 days \*/

?\>

…

And to create a link I would then add this code to any page:

if (isset($\_COOKIE[‘afilCookie’])) {  
$cookie = $\_COOKIE[‘afilCookie’];  
echo ‘\<a href="[http://order.examplesite.com?afil=](http://order.examplesite.com?afil=)’ . $cookie . ‘" class=“button”\>Order Now\</a\>’;  
}

**Can anyone help me with creating the correct code in HUBL to do all of this?**

**1. Add a cookie with the key/value from the URL query string (parameter).**

**2. Pull the cookie value to use as a variable in a link.**

I am very unfamiliar with it and haven’t been able to find any posts that match this request.

---

<div class="post-metadata">

**Author:** ![REVEALiO](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/revealio/32/99678_2.png) [@REVEALiO](https://community.hubspot.com/u/REVEALiO)\
**Post date:** [January 10, 2017, 12:06am UTC](https://community.hubspot.com/t/set-cookie-from-url-parameter-and-using-it-in-html-on-a-different-page/680/2 "2017-01-10T00:06:54Z")

</div>

HubSpot has told me that there is no way to set a cookie using PHP, Javascript, jQuery, or HubL. If anyone knows otherwise please let me know.

---

<div class="post-metadata">

**Author:** ![ndwilliams3](https://avatars.discourse-cdn.com/v4/letter/n/c4cdca/32.png) [@ndwilliams3](https://community.hubspot.com/u/ndwilliams3)\
**Post date:** [January 10, 2017, 3:01pm UTC](https://community.hubspot.com/t/set-cookie-from-url-parameter-and-using-it-in-html-on-a-different-page/680/3 "2017-01-10T15:01:51Z")

</div>

I don’t see why you could not use JavaScript to set a cookie?

```auto
function setCookie(cname, cvalue, exdays) {
 var d = new Date();
 d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
 var expires = "expires="+d.toUTCString();
 document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}

setCookie("afil", "{{ request.query_dict.afil }}", 30);

```

I’d give it a try and see if it works!

---

<div class="post-metadata">

**Author:** ![REVEALiO](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/revealio/32/99678_2.png) [@REVEALiO](https://community.hubspot.com/u/REVEALiO)\
**Post date:** [January 12, 2017, 12:51am UTC](https://community.hubspot.com/t/set-cookie-from-url-parameter-and-using-it-in-html-on-a-different-page/680/4 "2017-01-12T00:51:50Z")

</div>

I asked Hubspot and they said that they support Javascript but not when related to cookies

---

<div class="post-metadata">

**Author:** ![stefen](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/stefen/32/29274_2.png) [@stefen](https://community.hubspot.com/u/stefen)\
**Post date:** [January 11, 2017, 2:38pm UTC](https://community.hubspot.com/t/set-cookie-from-url-parameter-and-using-it-in-html-on-a-different-page/680/5 "2017-01-11T14:38:06Z")

</div>

To get just the value from a specific query field you can use this HubL:

`{{ request.query.get('afil') }}`

So if your URL looks like this: _[example.com/page?afil=randomkey](http://example.com/page?afil=randomkey)_

The code will return the string “randomkey”. Of course you could also just do this without HubL at all and with just vanilla javascript.
