At the office we went facing the issue of setting server-side snacks - many of them. Right we now have a PHP script, the only reason for which would be to set a cookie around the client for the domain. This occurs greater than 'normal' demands towards the server (that is running an application), so we have talked about moving it to the own server. This is an Apache server, most likely devoted, with one PHP script 3 lines lengthy, just running again and again again.

Surely there has to be a quicker, better method of carrying this out, instead of setting up the entire PHP atmosphere. Essentially, I want something super simple that may sit around all day long/evening doing the next:

  1. See if a particular cookie is placed, and
  2. In the event that cookie isn't set, grow it having a random hash (at this time it is a simple md5(microtime))

Any suggestions?

You could use mod_headers (usually obtainable in the default install) to by hand create a Set-Cookie header and emit it -- no programming needed as lengthy as it is exactly the same cookie each time. Something similar to this might operate in an .htaccess file:

Header add Set-Cookie "foo=bar; Path=/; Domain=.foo.com; Expires=Sun, 06 May 2012 00:00:00 GMT"

However, this will not meet your needs. There is no code here. It is simply a stupid header. It cannot develop the brand new random value you will want, also it can't adjust the expire date out of the box standard practice.

This is an Apache server, most likely devoted, with one PHP script 3 lines lengthy, just running again and again again. [...] Surely there has to be a quicker, better method of carrying this out, instead of setting up the entire PHP atmosphere.

Are you currently using APC or any other bytecode cache? If that's the case, there's very little startup cost. Because you are speaking about establishing a whole server for this purpose, it may sound as if you control the server too. Which means that you are able to switch off [cde] for a lesser startup hit.

Really though, if everything script does is building an md5 hash and setting a cookie, it will be blisteringly fast, particularly if it's mod_php. Do you know, though benchmarking and testing, the script is not carrying out in addition to you would like? If that's the case, are you able to share individuals benchmarks around?

It might be interesting to understand why do you consider you'll need extra server - would you really possess a bottle neck for producing the cookie or elsewhere ? Could it be the log writing as demands happen alot ? ajax polling ? Client data transfer speed ?

Atleast to begin with, i'd look some thing efficient than fetching time for you to create the "random hash". For instance, about this apple i7 laptop i've, producing 999999 md5 hashes from microtime takes roughly about 4 seconds and doing same factor with random amounts is second faster (not implementing a seeding of rand into consideration).

Then, for opening/and shutting of socket into consideration, just moving your script (which is probably already really fast - that's, not understanding the way your pages take that into consideration), you'll finish up really slowing down lower the demands. Really, since i have re-read your question, it can make me believe that your cookie setter script has already been a devoted page ? Or would you just "include" into real content offered by another php script? Otherwise, try that approach. Also this could advantageous for those who have default logging rules for apache, if snacks are occur on own page, your apache will log a row for your as well as in high load systems, this can cumulate to total io time spend by apache.

Also, take into account that testing if cookie is placed after which setting it, may be reduced than simply to vigorously place it always even when cookie is available or otherwise ?

But overall, i do not think you'd need to setup a server simply to offload cookie generation without learning more about the way you handle the snacks now.. Unless of course you do something really nasty.