# Set cookie in http request AND response

**URL:** <https://community.f5.com/t/set-cookie-in-http-request-and-response/51628>\
**Category:** Technical Forums\
**Tags:** irules, dev, devops, application-delivery\
**Created:** [January 28, 2013, 6:21pm UTC](https://community.f5.com/t/set-cookie-in-http-request-and-response/51628 "2013-01-28T18:21:26Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joern\_Oltmann](https://avatars.discourse-cdn.com/v4/letter/j/e9bcb4/32.png) [@Joern\_Oltmann](https://community.f5.com/u/Joern_Oltmann)\
**Post date:** [January 28, 2013, 6:21pm UTC](https://community.f5.com/t/set-cookie-in-http-request-and-response/51628/1 "2013-01-28T18:21:26Z")

</div>

Hi all,

this is my first Topic, so sorry if somthing wrong 😉

I would like to set a cookie (if not set) in the http request. So the application see if the user are new.

The same cookie I would like to set in the http response (if not set). Erveryone an idea?

My first iRule, but not finished yet shows:

Rename a cookie by inserting a new cookie name with the same value as the original. Then remove the old cookie.

when HTTP\_REQUEST {

Check if old cookie exists in request

if { [HTTP::cookie exists “\_global”] } {

everything ok, cookie exists

} else {

TODO: create unique session

set new\_session = [UnixTimeWithNanoSeconds]

Flag the request / session as new (for other systems)

HTTP::header insert “X-Global-Session” “New”

Insert new global cookie with session value

HTTP::cookie insert name “\_global” value $new\_session

}

}

Set cookie if a new Cookie was generated

when HTTP\_RESPONSE {

TODO: check if new session

if { $new\_session \> 0 } {

insert new global cookie and transport it to the client

HTTP::cookie insert name “\_global” value $new\_session

reset state

set new\_session = 0

}

}

Thanks for help

Joern

---

<div class="post-metadata">

**Author:** ![The\_Bhattman](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/the_bhattman/32/13958_2.png) [@The\_Bhattman](https://community.f5.com/u/The_Bhattman)\
**Post date:** [January 28, 2013, 10:45pm UTC](https://community.f5.com/t/set-cookie-in-http-request-and-response/51628/2 "2013-01-28T22:45:19Z")

</div>

Hi Joern,

What is the ultimate goal of the iRule if you don’t mind me asking? Is the application trying the track HTTP\_Header for a new client? Are you trying to establish persistence?

=Bhattman=

---

<div class="post-metadata">

**Author:** ![Joern\_Oltmann](https://avatars.discourse-cdn.com/v4/letter/j/e9bcb4/32.png) [@Joern\_Oltmann](https://community.f5.com/u/Joern_Oltmann)\
**Post date:** [January 29, 2013, 10:04am UTC](https://community.f5.com/t/set-cookie-in-http-request-and-response/51628/3 "2013-01-29T10:04:43Z")

</div>

… the whole irule tries to solve 2 things:

1) generate a global tracking session which is already created within the first request. it will never be used for authentication

2) provide a HTTP header for any system behind the f5 to mark the global tracking session as new

---

<div class="post-metadata">

**Author:** ![Joern\_Oltmann](https://avatars.discourse-cdn.com/v4/letter/j/e9bcb4/32.png) [@Joern\_Oltmann](https://community.f5.com/u/Joern_Oltmann)\
**Post date:** [January 29, 2013, 10:09am UTC](https://community.f5.com/t/set-cookie-in-http-request-and-response/51628/4 "2013-01-29T10:09:06Z")

</div>

Additional question: How can i generate in an iRule a unique session\_id?

---

<div class="post-metadata">

**Author:** ![hoolio](https://avatars.discourse-cdn.com/v4/letter/h/f6c823/32.png) [@hoolio](https://community.f5.com/u/hoolio)\
**Post date:** [January 29, 2013, 12:13pm UTC](https://community.f5.com/t/set-cookie-in-http-request-and-response/51628/5 "2013-01-29T12:13:32Z")

</div>

[http://devcentral.f5.com/Community/GroupDetails/tabid/1082223/aff/5/afv/topic/aft/2163201/afc/2263687/Default.aspx](http://devcentral.f5.com/Community/GroupDetails/tabid/1082223/aff/5/afv/topic/aft/2163201/afc/2263687/Default.aspx)

Here are two options. The rand option should be more efficient but not cryptographically secure. You can compare the CPU usage using the timing command.

Use rand() to generate a psuedo-random 8 digit number (not cryptographically secure)

format %010s [expr { int(100000000 \* rand()) }]

Use AES::key to generate a cryptographically secure unique ID

set uid [string range [AES::key 256] 8 end]

Use timing to do a performance comparison:

> **[DevCentral](https://community.f5.com/)**
>
> DevCentral is an online technical community where engineers and practitioners collaborate, share knowledge, and solve problems related to F5 technologies and industry trends.

Aaron

---

<div class="post-metadata">

**Author:** ![hoolio](https://avatars.discourse-cdn.com/v4/letter/h/f6c823/32.png) [@hoolio](https://community.f5.com/u/hoolio)\
**Post date:** [January 29, 2013, 12:14pm UTC](https://community.f5.com/t/set-cookie-in-http-request-and-response/51628/6 "2013-01-29T12:14:30Z")

</div>

…

---

<div class="post-metadata">

**Author:** ![Kevin\_Stewart](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/kevin_stewart/32/15267_2.png) [@Kevin\_Stewart](https://community.f5.com/u/Kevin_Stewart)\
**Post date:** [January 29, 2013, 2:02pm UTC](https://community.f5.com/t/set-cookie-in-http-request-and-response/51628/7 "2013-01-29T14:02:15Z")

</div>

Expanding on Aaron’s recommendation, I typically use the concatenation of two AES functions to return a unique ID:

Ex.

set session\_key “[string range [AES::key 128] 15 end][string range [AES::key 128] 15 end]”

And here’s something that might work for you:

```tcl
when HTTP_REQUEST {
if { not ( [HTTP::cookie exists _global] ) } {
set session_key "[string range [AES::key 128] 15 end][string range [AES::key 128] 15 end]"
HTTP::header insert "X-GLOBAL-Session" "New"
HTTP::cookie insert name "_global" value $session_key
}
}
when HTTP_RESPONSE {
if { [info exists session_key] and $session_key ne "" } {
HTTP::cookie insert name "_global" value $session_key
unset session_key
}
}

```

---

<div class="post-metadata">

**Author:** ![Joern\_Oltmann](https://avatars.discourse-cdn.com/v4/letter/j/e9bcb4/32.png) [@Joern\_Oltmann](https://community.f5.com/u/Joern_Oltmann)\
**Post date:** [January 30, 2013, 7:53am UTC](https://community.f5.com/t/set-cookie-in-http-request-and-response/51628/8 "2013-01-30T07:53:08Z")

</div>

its just the possibility for any other system (including i.e. monitoring like munin) to gain the information about new created global sessions somewhere in the request

---

<div class="post-metadata">

**Author:** ![Joern\_Oltmann](https://avatars.discourse-cdn.com/v4/letter/j/e9bcb4/32.png) [@Joern\_Oltmann](https://community.f5.com/u/Joern_Oltmann)\
**Post date:** [January 30, 2013, 12:42pm UTC](https://community.f5.com/t/set-cookie-in-http-request-and-response/51628/9 "2013-01-30T12:42:50Z")

</div>

Thanks all for help,

Someone suggestions for improvement, now I will use this iRule.

```tcl
when RULE_INIT {
  log local0. "Running RULE_INIT of iRule 'global_tracking_cookie'"
   Global Session Name
  set static::globalSessionName "_global"
   Session (Idle) timeout in seconds (10 year = 31536000 seconds)
  set static::globalSessionTimeout 31536000
   Header for New Global Session
  set static::globalSessionHeader "X-Global-Session"
  set static::globalSessionHeaderValueNew "new"
  log local0. "iRule 'global_tracking_cookie' initialized: sessionName($static::globalSessionName), sessionTimeout($static::sessionTimeout)"
}

when HTTP_REQUEST {
  if { not [HTTP::cookie exists $static::globalSessionName] } {
     INFO: generate global tracking session
     main_domain like "wlw.de"
    set main_domain_pos 2
    if { [string tolower [HTTP::host]] ends_with ".co.uk" } {
       main_domain is "wlw.co.uk"
      incr main_domain_pos 1
    }
    if { [string tolower [HTTP::host]] contains "onlinetest" } {
       main_domain is "onlinetest.wlw.(de|co.uk)"
      incr main_domain_pos 1
    } elseif { [string tolower [HTTP::host]] contains ".webdev." } {
       main_domain is "preview.webdev.wlw.(de|co.uk)"
      incr main_domain_pos 2
    }
    set main_domain [domain [HTTP::host] $main_domain_pos]
    set new_session_value "[string range [AES::key 128] 15 end][string range [AES::key 128] 15 end]"
     set http header and cookie
    HTTP::header insert $static::globalSessionHeader $static::globalSessionHeaderValueNew
    HTTP::cookie insert name $static::globalSessionName value $new_session_value
     cleanup
    unset main_domain_pos
  }
}

when HTTP_RESPONSE {
  if { [info exists new_session_value] } {
     INFO: transport the new global tracking cookie to the client
    HTTP::cookie insert name $static::globalSessionName value $new_session_value domain $main_domain
    HTTP::cookie expires $static::globalSessionName $static::globalSessionTimeout
     cleanup
    unset main_domain new_session_value
  }
}

```
