random image with session

Discussion in 'Programming' started by paprikb, Apr 4, 2007.

  1. #1
    i have a script random image and it's work OK, image is changing when user refresh page...
    but I need a script which display random images with sessions, image change when user close browser...
    how make this script with session...

    Thanx!

    script:
    <?php

    $path_to_images = "PATH"; // path for images
    $default_img = "test.png"; // default image, when error on page

    function getRandomImage($path, $img) {

    if ( $list = getImagesList($path) ) {
    mt_srand( (double)microtime() * 1000000 );
    $num = array_rand($list);
    $img = $list[$num];
    }
    return $path . $img;
    }

    function getImagesList($path) {
    $ctr = 0;
    if ( $img_dir = @opendir($path) ) {
    while ( false !== ($img_file = readdir($img_dir)) ) {
    // formati slik, ki jih prepozna
    if ( preg_match("/(\.gif|\.jpg|\.png)$/", $img_file) ) {
    $images[$ctr] = $img_file;
    $ctr++;
    }
    }
    closedir($img_dir);
    return $images;
    }
    return false;
    }

    ?>

    <img src="<?php echo getRandomImage($path_to_images, $default_img) ?>" alt="">
     
    paprikb, Apr 4, 2007 IP
  2. ewriter

    ewriter Banned

    Messages:
    590
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #2
    use if(!isset($_SESSION['id'])) it will help you

    call function when this variable is not set. So when you refresh the page it wont change. But when you close the explorer it will.
     
    ewriter, Apr 4, 2007 IP