Page 1 of 1

Display a Random Image from a Folder Using Php

Posted: Fri Feb 29, 2008 12:23 pm
by francisjeffy
Display a Random Image from a Folder

-> This Code Pics an image from a folder Randomly and displays it
-> Image is displayed randomly on each page refresh.



The Code

Code: Select all

$image_file_path = './testimg/';
$d = dir($image_file_path) or die("Wrong path: $image_file_path");
while (false !== ($entry = $d->read())) {
    if($entry != '.' && $entry != '..' && !is_dir($dir.$entry))
        $Images[] = $entry;
}
$d->close();
$max = count($Images) - 1;
$n = rand(0,$max);
$img = $Images[$n];
echo "<img src='$image_file_path$img'>";
Line by Line Explanation

Code: Select all

<?php
session_start();
 
# The path to the folder which has the images is defined in the variable $image_file_path
$image_file_path = './testimg/';
 
# Open the directory and read the files from the folder and store it to an array $Images[]
$d = dir($image_file_path) or die("Wrong path: $image_file_path");
while (false !== ($entry = $d->read())) {
    if($entry != '.' && $entry != '..' && !is_dir($dir.$entry))
        $Images[] = $entry;
}
$d->close();
 
# Get the total number of images into the variable $max and minus 1, since array index starts with 0
$max = count($Images) - 1;
 
# Get a random number of the array index values
$n = rand(0,$max);
 
# Store random image to a variable
$img = $Images[$n];
 
# Display the random image
echo "<img src='$image_file_path$img'>";
?>

-> This code displays the image randomly on the first page load,
and does not change the Image on Each Refresh.
-> And there is an option of Session time out, which is used to change images on Refresh.
-> Sessions are used to store random image names, And can be cleared in logout session unset




The Code

Code: Select all

$image_file_path = './testimg/';
$d = dir($image_file_path) or die("Wrong path: $image_file_path");
while (false !== ($entry = $d->read())) {
    if($entry != '.' && $entry != '..' && !is_dir($dir.$entry))
        $Images[] = $entry;
}
$d->close();
$max = count($Images) - 1;
$n = rand(0,$max);
$ip=$_SERVER['REMOTE_ADDR'];
if($_SESSION['ip'] == ''){
    $_SESSION['ip'] = $ip;
    $_SESSION['img'] = $Images[$n];
}
if($_SESSION['timeStart'] == '' and $_SESSION['timeEnd'] == ''){
    $_SESSION['timeStart'] = mktime(date("H"), date("i"), date("s"), date("m"), date("d"), date("Y"));
    $_SESSION['timeEnd'] = $_SESSION['timeStart'] + (3 * 2);    
}
$currentTime = mktime(date("H"), date("i"), date("s"), date("m"), date("d"), date("Y"));
echo "<br><br>".$_SESSION['img'];
if($currentTime >= $_SESSION['timeEnd']){
    unset($_SESSION['timeStart']);
    unset($_SESSION['timeEnd']);
    unset($_SESSION['ip']);
    unset($_SESSION['img']);
}

Line by Line Explanation

Code: Select all

<?php
session_start();
 
# The path to the folder which has the images is defined in the variable $image_file_path
$image_file_path = './testimg/';
 
# Open the directory and read the files from the folder and store it to an array $Images[]
$d = dir($image_file_path) or die("Wrong path: $image_file_path");
while (false !== ($entry = $d->read())) {
    if($entry != '.' && $entry != '..' && !is_dir($dir.$entry))
        $Images[] = $entry;
}
$d->close();
 
# Get the total number of images into the variable $max and minus 1, since array index starts with 0
$max = count($Images) - 1;
 
# Get a random number of the array index values
$n = rand(0,$max);
 
# get user ip address
$ip=$_SERVER['REMOTE_ADDR'];
 
# Store ip address to session
# Store image name to session while session ip is not set
if($_SESSION['ip'] == ''){
    $_SESSION['ip'] = $ip;
    $_SESSION['img'] = $Images[$n];
}
 
# Set time start and time end to session while time session are not set
if($_SESSION['timeStart'] == ''){
    $_SESSION['timeStart'] = mktime(date("H"), date("i"), date("s"), date("m"), date("d"), date("Y"));
    # Specify Time end here. For example it is (3 * 2) = 6 seconds here
    $_SESSION['timeEnd'] = $_SESSION['timeStart'] + (3 * 2);    
}
 
# Get Current Time
$currentTime = mktime(date("H"), date("i"), date("s"), date("m"), date("d"), date("Y"));
 
# Display Image name
echo "<br><br>".$_SESSION['img'];
 
# Unset time session while current time > time end
if($currentTime >= $_SESSION['timeEnd']){
    unset($_SESSION['timeStart']);
    unset($_SESSION['ip']);
}
?>
Hope it was helpful to you, and if you know a way to unset php sessions on window close, please let me know..

jeF..

Re: Display a Random Image from a Folder Using Php

Posted: Fri Feb 29, 2008 12:33 pm
by Jonah Bron
Nice. Send to Code Critique, and then Code Snippets.

Sessions are automatically deleted when the window closes.

Re: Display a Random Image from a Folder Using Php

Posted: Fri Feb 29, 2008 12:43 pm
by Zoxive
PHPyoungster wrote:Sessions are automatically deleted when the window closes.
Incorrect. They will be deleted if you clear cookies when the browser closes. (Firefox can do this)

They time out after the specified time, which you can set in php.ini

Critique:
Make it a function that returns the image.

Re: Display a Random Image from a Folder Using Php

Posted: Fri Feb 29, 2008 3:55 pm
by Jonah Bron
Hum. And all this time I thought the get deleted automatically. Do they get deleted when you navigate away from the site?

Re: Display a Random Image from a Folder Using Php

Posted: Mon Mar 03, 2008 3:26 pm
by francisjeffy
Hi,

Sessions do not get deleted automatically. Conditions for a Sessions to get deleted are,

-> session unset:- All or selected sessions can be deleted by using session unset() or session destroy.
-> Session time out:- A life span can be defined for a session, and when the session reaches its defined life span the session automatically get deleted.

These are the two ways by which a programmer can delete the sessions, through the program (As far as I know).

The user can delete the session by clearing the browser cookies

If you have some further insights, please share it.

jeF..