display variable image using cookies

Have you considered using PHP instead? I feel that it would be a lot more straightfoward. Use setcookie and then simple if and elseif statements to check what the cookie is, and display a corresponding image.

This seems a lot simpler than what you’re trying to do with JavaScript, but maybe that’s just because I use PHP so much more often…

So maybe something like

$value = "something"; /* sets the value of the cookie */
setcookie("imagedisplay", $value, time()+3600);  /* expire in 1 hour */

on the previous page, and then to check it just use:

if($_COOKIE["imagedisplay"] == "something") {
$imageurl="ABC.jpg";
}
elseif($_COOKIE["imagedisplay"] == "something else") {
$imageurl="DEF.jpg";
}
else {
$imageurl="GHI.jpg";
}

And then use some more PHP to output the corresponding URL as the source of the image. Something like

<img src="<? echo $imageurl; ?>" />

Haven’t tested this code, but it should work.

Leave a Comment