random randomizer question

  • celandine
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 2008
  • Loc: Belgrade, Serbia

Post 3+ Months Ago

hey guys, long time no see... hahah I say that every time :D

ok, practical question, though I'm not sure what forum it should go into so I thought I'd start here... when you use a randomizer to display, say, an array of images in your forum signature, is there any way to attach links to those signatures? So that when a particular image appears, it can be clickable and lead to a particular page.

Does anyone know a randomizer that allows for this kind of functionality?

thanks in advance! a friend's happiness depends on me finding this answer :D
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

I don't know if this would help but I wrote this script about a year or so ago to answer a similar question in this forum.

PHP Code: [ Select ]
<?php
/**
 * Random image rotator
 */
 
// Function to generate different IDs
function get_rands($min, $max, $amount)
{
    $amount = (int) $amount;
    $rands = array();
    $rand = false;
   
    for($i = 0; $i < $amount; ++$i)
    {
        while(!$rand || in_array($rand, $rands))
        {
            $rand = rand($min, $max);
        }
       
        $rands[] = $rand;
    }
   
    return $rands;
}
 
/** The images that would be shown.
 *
 * Editing Explanation:
 * 'source_to_image' => 'website_link'
 */
$images = array(
        'image1.png' => 'http://www.google.com',
        'image2.png' => 'http://www.yahoo.com',
        'image3.png' => 'http://www.www.com',
        'image4.JPG' => 'http://www.ozzu.com'
        );
 
//** You don't have to edit below this line **//
 
// Initiating the $image array
$image = array();
 
// ID that would be provided for each image
$i = 0;
 
// The KEY is the image and the $value is the link
foreach($images as $key => $value)
{
    $image[$i]["LINK"] = $value;
    $image[$i]["IMAGE"] = $key;
    ++$i;
}
 
// Get 3 different IDs
$id = get_rands(0, $i-1, 3);
 
// Printing the random IDs for debug... remove or comment when not needed
print_r($id);
 
// Generating the HTML part of the script
foreach($id as $im_id)
{
   echo "<a href=\"{$image[$im_id]['LINK']}\"><img src=\"{$image[$im_id]['IMAGE']}\" alt=\"{$image[$im_id]['IMAGE']}\" /></a><br />\n";
}
?>
  1. <?php
  2. /**
  3.  * Random image rotator
  4.  */
  5.  
  6. // Function to generate different IDs
  7. function get_rands($min, $max, $amount)
  8. {
  9.     $amount = (int) $amount;
  10.     $rands = array();
  11.     $rand = false;
  12.    
  13.     for($i = 0; $i < $amount; ++$i)
  14.     {
  15.         while(!$rand || in_array($rand, $rands))
  16.         {
  17.             $rand = rand($min, $max);
  18.         }
  19.        
  20.         $rands[] = $rand;
  21.     }
  22.    
  23.     return $rands;
  24. }
  25.  
  26. /** The images that would be shown.
  27.  *
  28.  * Editing Explanation:
  29.  * 'source_to_image' => 'website_link'
  30.  */
  31. $images = array(
  32.         'image1.png' => 'http://www.google.com',
  33.         'image2.png' => 'http://www.yahoo.com',
  34.         'image3.png' => 'http://www.www.com',
  35.         'image4.JPG' => 'http://www.ozzu.com'
  36.         );
  37.  
  38. //** You don't have to edit below this line **//
  39.  
  40. // Initiating the $image array
  41. $image = array();
  42.  
  43. // ID that would be provided for each image
  44. $i = 0;
  45.  
  46. // The KEY is the image and the $value is the link
  47. foreach($images as $key => $value)
  48. {
  49.     $image[$i]["LINK"] = $value;
  50.     $image[$i]["IMAGE"] = $key;
  51.     ++$i;
  52. }
  53.  
  54. // Get 3 different IDs
  55. $id = get_rands(0, $i-1, 3);
  56.  
  57. // Printing the random IDs for debug... remove or comment when not needed
  58. print_r($id);
  59.  
  60. // Generating the HTML part of the script
  61. foreach($id as $im_id)
  62. {
  63.    echo "<a href=\"{$image[$im_id]['LINK']}\"><img src=\"{$image[$im_id]['IMAGE']}\" alt=\"{$image[$im_id]['IMAGE']}\" /></a><br />\n";
  64. }
  65. ?>


See if that helps you.

NOTE: There is a better way to implement the HTML part of the rotator (Using a loop if you are doing more than one image like this example [it rotates 3 images])... I'm not going to fix that unless I know that this is what you want (Or close enough that you can modify it to do what you want).

[EDIT:] I fixed it up anyway...
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

The first thing that comes to mind is to rewrite the URI at the server to a script for both the image and the link.

When a request is made for the image, the script will make a note of the IP address, the browser, and the referrer of the request. It will generate a key for use when a link is clicked. If there is no referrer it will return the image for a default banner and make no record.

The random image will be returned immediately along with headers that prevent the image from being cached, and then the destination URL will be saved somewhere using that generated key.

When the visitor clicks the link the browser should send the same IP, referer, and browser identifier it did when it made the request for the image, so I can rewrite the request to a script that looks up the URL using the generated key and then redirects the visitor to that URL.
  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

I just threw something together using sessions.

Image
Code: [ Select ]
[url=http://travisperson.com/forum.php][img]http://travisperson.com/forum.php?a=image[/img][/url]

There are three images (Ozzu,Yahoo,Google). Each will direct you to their respected websites.

PHP Code: [ Select ]
<?php
session_start();
 
$action = (isset($_GET['a'])) ? $_GET['a'] : '';
 
$images = array(
        'image_1.jpg',
        'image_2.jpg',
        'image_3.jpg'
        );
 
$url = array(
      'http://www.google.com',
      'http://www.yahoo.com',
      'http://www.ozzu.com'
      );
 
if($action == 'image')
{  
    $id = rand(0, count($images)-1);
   
   $_SESSION['id'] = $id;
   
    $imagepath=$images[$id];
   $image=imagecreatefromjpeg($imagepath);
   
   header('Content-Type: image/jpeg');
   
   imagejpeg($image);
}
else
{
   header("Location: ".$url[$_SESSION['id']]); 
}
 
?>
  1. <?php
  2. session_start();
  3.  
  4. $action = (isset($_GET['a'])) ? $_GET['a'] : '';
  5.  
  6. $images = array(
  7.         'image_1.jpg',
  8.         'image_2.jpg',
  9.         'image_3.jpg'
  10.         );
  11.  
  12. $url = array(
  13.       'http://www.google.com',
  14.       'http://www.yahoo.com',
  15.       'http://www.ozzu.com'
  16.       );
  17.  
  18. if($action == 'image')
  19. {  
  20.     $id = rand(0, count($images)-1);
  21.    
  22.    $_SESSION['id'] = $id;
  23.    
  24.     $imagepath=$images[$id];
  25.    $image=imagecreatefromjpeg($imagepath);
  26.    
  27.    header('Content-Type: image/jpeg');
  28.    
  29.    imagejpeg($image);
  30. }
  31. else
  32. {
  33.    header("Location: ".$url[$_SESSION['id']]); 
  34. }
  35.  
  36. ?>


Its not to pretty but it works ;)
  • celandine
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 2008
  • Loc: Belgrade, Serbia

Post 3+ Months Ago

wow thanks guys!!!! I feel so embarrassed I totally forgot about this question. I've gotta try to make one of these options work :D

thank you so much!!!
  • celandine
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 2008
  • Loc: Belgrade, Serbia

Post 3+ Months Ago

joebert wrote:
The first thing that comes to mind is to rewrite the URI at the server to a script for both the image and the link.

When a request is made for the image, the script will make a note of the IP address, the browser, and the referrer of the request. It will generate a key for use when a link is clicked. If there is no referrer it will return the image for a default banner and make no record.

The random image will be returned immediately along with headers that prevent the image from being cached, and then the destination URL will be saved somewhere using that generated key.

When the visitor clicks the link the browser should send the same IP, referer, and browser identifier it did when it made the request for the image, so I can rewrite the request to a script that looks up the URL using the generated key and then redirects the visitor to that URL.

ok I read all of that twice and I have no idea what it says :D But I'm very grateful for the effort, Joe.

I'm gonna try and go with bogey's and spoof's option parallel, and see which one gets me further. (though seeing as I'm currently spending approx 14 hours a day on my day job, it's probably gonna be a long and painful process)
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

Quote:
ok I read all of that twice and I have no idea what it says But I'm very grateful for the effort, Joe.


A lot of times when I reply to topics like this without posting any code, I'm generally talking to the other people reading who may be looking for something to write code for. :D

--

I was worried about an issue that might happen where someone looks at a page, and then opens another page that has the dynamic image on it too.

The first image may say "yahoo", but when they open a new tab and the image in that new tab says "google", if the server wasn't keeping track of what went where, the visitor could be taken to the google url when they go back into the first page and click yahoo.

So I thought out a system to work around that and tossed the idea out there for anyone who might want to implement it. :)

It appears though, at least with Spoofs example and my copy of Opera 10 here, that when the new page gets loaded in a new tab, the image will also change in the old tab. So there would be no confusion. Technically, it's going to be the same image in each tab, I don't know which browsers are going to re-render the first tab with the new image after simply switching between tabs though.

Post Information

  • Total Posts in this topic: 7 posts
  • Users browsing this forum: No registered users and 58 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.