开发者

How do you include a cookie in an HTTP Response (e.g. an image request)?

开发者 https://www.devze.com 2023-04-05 09:01 出处:网络
I was wondering how companies like Double-Click include a cookie in their image responses to track users. Similarly, how do the images (e.g. smart pixels) send information back to their servers?

I was wondering how companies like Double-Click include a cookie in their image responses to track users. Similarly, how do the images (e.g. smart pixels) send information back to their servers?

Please provide a scripting example if possible (any language 开发者_如何学编程is okay) [note: if this is resolve doings something server side, please describe how this would be accomplished using APACHE].

Cheers, Rob


How do they include a cookie ? Configure the server, probably via script to send cookies with the responses. Images are http requests, that follow the http protocol, there is nothing magical about them.

"Smart pixels" convey their information simply via the request the browser must send to the server in order to load the image. Information about the user/browser, can be gathered via javascript and embedded in the url.


To do this in php, you'd use the setCookie function.

<?php
$value = 'something from somewhere';

setcookie("TestCookie", $value);
setcookie("TestCookie", $value, time()+3600);  /* expire in 1 hour */
setcookie("TestCookie", $value, time()+3600, "/~rasmus/", ".example.com", 1);
?>

That code was taken from the php doc I referenced above. Basically this adds the Set-Cookie to the HttpResponse header like: Set-Cookie: UserID=JohnDoe; Max-Age=3600; Version=1

See http://en.wikipedia.org/wiki/List_of_HTTP_header_fields and search for Set-Cookie

ALSO, in scripting languages, like PHP, make sure you set the header before you render any content. This is because the HTTP Headers are the first thing sent in the response, so as soon as you write content, the headers should've already been written.

Another quote from the PHP:setcookie doc:

Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including and tags as well as any whitespace.

0

精彩评论

暂无评论...
验证码 换一张
取 消