Forum Moderators: coopster

Message Too Old, No Replies

How to place a php program into <img src="..."> and let it read cookie

It returns empty string when loading as an image

         

iProgram

10:17 am on Apr 29, 2004 (gmt 0)

10+ Year Member



For example, a cookie was set like this:
[PHP] setcookie("cookie_name", "test.gif", time()*60*60*24);[/PHP]
Now I want to use read_cookie.php to display test.gif:
[PHP]
:read_cookie.php
<?
$value=$_COOKIE["cookie_name"];
if($value=='')
{
$value="unknown.gif";
}
//$value should equal to "test.gif"
//Now display this picture
Header ("Location: $value");
?>
[/PHP]
When loading read_cookie.php directly, it can display test.gif without any problem. However, I want to display this picture in some HTML files, using the following method:
[PHP]
:test.html
<html>
<body>
test
<img src="read_cookie.php">
</body>
<html>
[/PHP]

The problem is, the picture displayed is unknown.gif, not test.gif. In another word, when calling read_cookie.php as an image of test.html, the $_COOKIE["cookie_name"] variable returns ' ', not test.gif. Why? How to solve this problem?

hakre

1:39 pm on Apr 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hi iprogram,

edit: i thought you're outputting the binary data in the script read_cookie.php. but you're just redirecting, so this will not help you.
in case the redirection is the problem - what i think right now - you'll need to output the file instead of redirecting to it.
but redirecting should not be the problem at all, because it's http conform. please check which http version you use (1.0 or 1.1). in 1.1 it's a need to use full pathes like http://www.your.dom/images/test.gif instead of simply test.gif. maybe that's your problem.

if you afterwards favorise a direct output without redirect, the code below might help you to get a start.

i think this is because "read_cookie.php" will return the so called content-type or mime-type text/html (default for php). you will need to replace it in the read_cookie.php script. add the following line in your script (it's important that this is done before any other output):


header('Content-type: image/gif');
[i]...
the output of binary data of the gif-file here
...[/i]

because i don't know your script in detail, this is only an assumption.

additionally you can return the size in bytes that way:


header("Accept-Ranges: bytes");
header("Content-Length: [i]number of bytes[/i]");

--hakre