[thelist] Cookies and images

Simon Perry thelist at si-designs.co.uk
Fri Feb 4 06:03:16 CST 2005


Joshua Olson wrote:

>>>Is there a way to read, through JavaScript, those cookies that may be
>>>returned with images?
>>>
>>>      
>>>
>>If they are cookies returned by the site / server hosting the 
>>page that 
>>your JavaScript is being served from I don't see why not. Normal 
>>JavaScript cookie handling should "just work" (TM).
>>    
>>
>
>Simon,
>
>Care to elaborate?  I tested this--hopefully an adequate test--but I didn't
>see anything helpful when I looked at document.cookie after the image had
>returned the cookie.
>
Joshua,

Hard to know what is going wrong for you without example code or better 
an online test case. I tested the theory on a site I'm working on and it 
works fine for me.

The code is as follows, if you would like a test address then contact me 
off list as I don't want the test page picked up by search engines.

Simon

PHP to set cookie and image;

<?php
// send compact privacy policy and policy link
header('P3P: CP="NID DSP NOI COR", 
policyref="http://www.domain.co.uk/w3c/p3p.xml"');

//set the cookie
setcookie("cfc_friend","{$_GET['id']}",mktime(0,0,0,date("m")+1,date("d"),date("Y")),'/','domain.co.uk',0);

// open the file in a binary mode
switch($_GET['img'])
  {
    case "460":
      $name = "../images/affiliate/postbox.gif";
        break;
    case "125":
      $name = "../images/affiliate/square125x.gif";
        break;
    case "120":
      $name = "../images/affiliate/rect120x240.gif";
        break;
    case"180":
      $name = "../images/affiliate/180x-logo.gif";
        break;
    }


$fp = fopen($name, 'rb');

// send the right headers
header("Content-Type: image/gif");
header("Content-Length: " . filesize($name));

// dump the picture and stop the script
fpassthru($fp);
fclose($fp);
?>

Test page;

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1" />
<title>Cookie Test</title>
<script type="text/javascript">
//<![CDATA[
<!--
function display_cookies()
  {
    alert(document.cookie);
    document.getElementById('textPad').value=document.cookie;
    }

// -->
//]]>
</script>
</head>
<body onload="display_cookies()">
<h1>Cookie Test</h1>
<h2>Image that sets the cookie</h2>
<p>
<img 
src="http://www.domain.co.uk/affiliate/aff_img.php?img=180&amp;id=1" 
alt="Image that sets the cookie" />
</p>
<h2>PHP $_COOKIE Array</h2>
<p>On first entry to this page this variable is not available to PHP.
If you refresh the page you will see that PHP has picked up the cookie 
too.</p>
<pre>
<?php
print_r($_COOKIE);
 ?>
</pre>
<h2>JavaScript document.cookie Value</h2>
<p>My testing shows that Javascript, unlike PHP, has access to the 
cookie on first page load.
The value of document.cookie should be shown in the text area below.</p>
<textarea rows="10" cols="30" name="textPad" id="textPad">

</textarea>
</body>
</html>


More information about the thelist mailing list