# Variable assignment in irule

**URL:** <https://community.f5.com/t/variable-assignment-in-irule/53621>\
**Category:** Technical Forums\
**Tags:** big-ip-access-policy-manager-apm, irules, application-delivery\
**Created:** [December 9, 2018, 12:20pm UTC](https://community.f5.com/t/variable-assignment-in-irule/53621 "2018-12-09T12:20:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chandru\_01](https://avatars.discourse-cdn.com/v4/letter/c/b5e925/32.png) [@Chandru\_01](https://community.f5.com/u/Chandru_01)\
**Post date:** [December 9, 2018, 12:20pm UTC](https://community.f5.com/t/variable-assignment-in-irule/53621/1 "2018-12-09T12:20:15Z")

</div>

Dear All,

I need to set an irule variable in the HTTP\_RESPONSE based on a condition, and make a decision on the next HTTP\_REQUEST based on the variable value and unset the variable.

The problem is that the variable value that was set on HTTP\_RESPONSE does not get reflected in the next HTTP\_REQUEST. What should I do to make the variable hold its value.

example:

when HTTP\_REQUEST { if { [info exists var] and $var eq 1 }{

do something

unset var } }

when HTTP\_RESPONSE { if { [HTTP::status] contains 302 and [HTTP::header “Location”] eq “/vdesk/hangup.php3” } { set var 1 } }

---

<div class="post-metadata">

**Author:** ![Faruk\_AYDIN](https://avatars.discourse-cdn.com/v4/letter/f/57b2e6/32.png) [@Faruk\_AYDIN](https://community.f5.com/u/Faruk_AYDIN)\
**Post date:** [December 9, 2018, 2:03pm UTC](https://community.f5.com/t/variable-assignment-in-irule/53621/2 "2018-12-09T14:03:57Z")

</div>

Hi Guy,

use a custom session variable in your iRule like the following:

```tcl
when HTTP_REQUEST { 
if { [ACCESS::session data get session.custom.var] eq "1" }{
    do something
    ACCESS::session data set session.custom.var ""
}
}
when HTTP_RESPONSE { 
if { [HTTP::status] contains 302 and [HTTP::header "Location"] eq "/vdesk/hangup.php3" } 
{   
    ACCESS::session data set session.custom.var "1"
}
}

```

---

<div class="post-metadata">

**Author:** ![stan\_piron](https://avatars.discourse-cdn.com/v4/letter/s/7bcc69/32.png) [@stan\_piron](https://community.f5.com/u/stan_piron)\
**Post date:** [December 9, 2018, 3:13pm UTC](https://community.f5.com/t/variable-assignment-in-irule/53621/3 "2018-12-09T15:13:24Z")

</div>

The problem with your code is the next request can be in a different tcp connection… so the variable is not shared between response and request…

Http response can be with http 1.0 version which means the tcp connection close just after response…

---

<div class="post-metadata">

**Author:** ![youssef1](https://avatars.discourse-cdn.com/v4/letter/y/ed655f/32.png) [@youssef1](https://community.f5.com/u/youssef1)\
**Post date:** [December 10, 2018, 1:15pm UTC](https://community.f5.com/t/variable-assignment-in-irule/53621/4 "2018-12-10T13:15:31Z")

</div>

Hi,

You have the opportunity to use the functionnality (OTP Verify) in vpe that allow you to activate 5 attempts. Or create via an empty box your need (verify your otp with loop:5)

First of you use F5 native solution? for OTP.

Why you can’t use OTP Verify that allow you 5 attempts…

regards
