# Cookie injection / verification / redirect

**URL:** <https://www.ruby-forum.com/t/cookie-injection-verification-redirect/158424>\
**Category:** NGINX\
**Created:** [January 23, 2009, 5:36am UTC](https://www.ruby-forum.com/t/cookie-injection-verification-redirect/158424 "2009-01-23T05:36:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Payam\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/59ef9b/32.png) [@Payam\_C](https://www.ruby-forum.com/u/Payam_C)\
**Post date:** [January 23, 2009, 5:36am UTC](https://www.ruby-forum.com/t/cookie-injection-verification-redirect/158424/1 "2009-01-23T05:36:24Z")

</div>

Hey Guys,

Im trying to see what the best way of doing this would be… any and  
all thoughts are greatly appreciated.

a) client --\> proxy --\> [cookie set?] if yes, proxy connection to  
$origin. call it a day  
b) client --\> proxy --\> [cookie set?] if no, set cookie, redirect  
client to proxy and see if the cookie is set, if No, keep looping; if  
yes, proxy connection to $origin.

I see that you can use the following ways to inject cookie into the  
header:

```
    add_header Cookie "mysuperdoopercookiehere";

```

or  
userid on;  
userid\_name uid;  
userid\_domain domainname\_here.com;

Using firefox cookie header add-on, ive been able to confirm that the  
cookie injection works just fine however, I haven’t a clue on how to  
properly write the redirect so that if the cookie is not set, set it  
and have the client re-establish connection with the cookie added to  
the header and if the connection has a cookie set then pass it to the  
proxy.

Any help with this would greatly be appreciated… ive been banging my  
head to my desk for the last 2 days.

Thanks in advance,  
Payam

---

<div class="post-metadata">

**Author:** ![Payam\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/59ef9b/32.png) [@Payam\_C](https://www.ruby-forum.com/u/Payam_C)\
**Post date:** [January 23, 2009, 6:13am UTC](https://www.ruby-forum.com/t/cookie-injection-verification-redirect/158424/2 "2009-01-23T06:13:14Z")

</div>

I haven’t tested this myself, but I believe it should do what you want.

if ($http\_cookie !~ ‘my\_cookie=some\_value’) {  
add\_header Cookie ‘my\_cookie=some\_value’;  
rewrite (.\*) $1 last;  
break;  
}

# statements hereafter will only be reached if the cookie is properly

set

I’m not sure the ‘break’ is necessary since the previous statement ends  
with  
‘last’, but I see it in examples quite a bit.

Be mindful that this will cause an infinite loop (and an eventual  
client-side error message) for browsers with cookie support turned off.  
It  
might be better to redirect users to something like /cookie\_added, then  
when  
a request comes in for /cookie\_added, rewrite the request internally to  
remove /cookie\_added from the URI and process as normal. You could even  
check the URI, and if it is for /cookie\_added but the cookie is not set,  
you  
could show a friendly warning message instructing the user that cookies  
are  
required for your site.

Hope that helps.

Nick
