-> 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'>";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']);
}
?>jeF..