Jump to content

running setcookie()


tarnold4

Recommended Posts

I have a script i was playing with below. I want to edit it using the setcookie() function to set a cookie with the random image script, using the $_COOKIE array, i want to be able to read the cookie if its available. Finally i want to create a test condition where a new random index is selected if the user has previously viewed the image and the cookie is available. I would love to know how to do this

 <html>
  <head>
  <title>JFQ Turnings</title>
  </head>
  <body bgcolor="#ffffff" text="#000000">
  <img src="jfqturnings.gif" alt="JFQ Turnings, coming soon." width="864" height="100">
  <br /><img src="<?php echo getRandomImage(); ?>" alt="" width="864" height="567">
  <?php
  echo getRandomImage();
 function getRandomImage()
 {
 $images = array("candlestick.jpg", "rollingpin.jpg", "table.jpg", "table2.jpg");
 $random = rand(0, 3);
 return $images[$random];
 }
 ?> 
 </body>
 </html>
Link to comment
Share on other sites

You seem to know the functions you already require.

setcookie('mycookie', 'random_image.jpg', time() + 3600);

if (!empty($_COOKE['mycookie']) {
    echo "$_COOKIE['mycookie'] has the value of " . $_COOKIE['mycookie'];
}
Edited by iarp
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.