Forum Discussion

Brent_Sachnoff_'s avatar
Brent_Sachnoff_
Icon for Nimbostratus rankNimbostratus
Dec 06, 2007

Assigning cookies only once

I'm trying to assign a client a cookie that will be psuedo random using:

 

when HTTP_RESPONSE {

 

expr srand([clock clicks])

 

set tracker [expr (rand() * 999) + 10]

 

HTTP::cookie insert name tracker value $tracker path "/"

 

}

 

 

This works fine and I can verify it on the HTTP_REQUEST. My problem is I only want to assign this cookie once and never do it again for this particular session. Is there a way to:

 

on request, check for cookie. If it does not exist, on response issue one out otherwise just log the current cookie.

 

 

Thanks,

 

 

Brent

4 Replies

  • You can use a local variable to track whether the cookie was presented in the client request:

    
    when HTTP_REQUEST {
       if {[HTTP::cookie exists tracker]}{
          set need_cookie 0
          log local0. "tracker cookie=[HTTP::cookie value tracker]"
       } else {
          set need_cookie 1
       }
    }
    when HTTP_RESPONSE {
       if {$need_cookie] }{
          expr srand([clock clicks])
          set tracker [expr (rand() * 999) + 10]
          HTTP::cookie insert name tracker value $tracker path "/"
       }
    }

    Aaron

  • I was trying to use a local variable set as the actual cookie if it existed and then checking that on the response.. For some reason I had no luck. This does work now though. Thanks!
  • Hi Aaron,

     

     

    In your example, is the reason you reseed with srand on every response to prevent the possibility of another iRule reseeding with the same seed and thus rand() producing the same results? What is the performance overhead of reseding on every http hit? Wouldn't using just [clock ticks] provide a sufficient unique id?

     

     

    Thanks!

     

     

    //Joe
  • Just realized that the cookie insert code will not be executed for each http hit (only the first hit for that browser session), so performance shouldn't be too much of an issue. Never mind!

     

     

    BTW, do you know the overhead with using [AES::key] to generate an unique id versus the rand method you outlined?

     

     

    Thanks! //Joe