Jump to content

Display a different image whenever a new user visits.


rodd

Recommended Posts

Hi, I've already posted my problem before and i thought it was solved, but later realised not hence a repost.

 

Currently i have a page where i want to display a different image every time a "NEW" user visits the page.

 

So.....user 1 sees image 1

        user 2 sees image 2

        user 3 sees image 3 etc etc....(all users from different locations)

 

At the moment when i refresh the page it works as i want it too. ie i see image 1 then after a refresh i see image to and so on.

 

BUT......When i clear my temp files it just takes me back to image 1 everytime. Which obviously means every visitor will land on image 1 too as it is the start of a session.

 

Is there a way to store cookies/sessions on the server to display a different image instead?

 

I don't know if im asking the impossible or not. I'm certainly glad for the help.

 

heres the site, www.plumbersnottingham.co.uk

 

Heres the code i've been using.

 

<?php
$img = array();
$img[] = '<img src="PLUMBER1.jpg" />';
$img[] = '<img src="PLUMBER2.jpg" />';
$img[] = '<img src="PLUMBER3.jpg" />';
$img[] = '<img src="PLUMBER4.jpg" />';
$img[] = '<img src="PLUMBER5.jpg" />';

if(!isset($_SESSION['image_display'])){
   $_SESSION['image_display'] = 0;
}else{
   $_SESSION['image_display']++;
   if($_SESSION['image_display'] > count($img)-1){
      $_SESSION['image_display'] = 0;
   }
}

echo $img[$_SESSION['image_display']];
?>

 

Thanks

does it matter if they see them in a special sequence?

 

if not, you can code it to grab a random image every time.

 

that way you don't have to worry about sessions/cookies at all.

 

if you need them to see a certain image, you could store the current image in a cookie, BUT if the user deletes private data or temporary internet files or spyware software erases the cookies after they leave your site, it will backfire.

 

 

Hi thanks for the replies,

 

I tried a random grab sequence but it tended to stick on the same image to often. Refreshing the page would often show image 4 twice in a row followed by 2 twice etc.... i need things to be more equal as these are potential clients phone numbers.

 

Thanks.

i agree if the IP changes it could theoretically cause problems.

 

but it's worth a try. There has to be a simpler way than using a database query everytime

 

the random images script may work fine, just might need some tweaking to root out the duplicate images.

 

also, how many images are we talking about??

 

Hi, here is the RANDOM code ive been using. Only  a few images maximum 5.

 

<?php
$img = array();
$img[] = '<img src="img1.jpg" />';
$img[] = '<img src="img2.jpg" />';
$img[] = '<img src="img3.jpg" />';
$img[] = '<img src="img4.jpg" />';
$img[] = '<img src="img5.jpg" />';
$max = count($img) - 1;
$count = rand(0,$max);
echo $img[$count];
?>

There's no way to do this perfectly in every situation. Personally, I think if you use the session one and they clear their cache, or if their IP address changes quickly enough for that not to work, then they're on their own. Too bad so sad.

<?php
$img = array();
$img[] = '<img src="img1.jpg" />';
$img[] = '<img src="img2.jpg" />';
$img[] = '<img src="img3.jpg" />';
$img[] = '<img src="img4.jpg" />';
$img[] = '<img src="img5.jpg" />';
echo $img[array_rand($img, 1)];
?>

 

How does that work for you?

 

EDIT: Just read your last post.  That could be more complicated than it's worth...may I ask why you'd need that?

Thanks Darkwater.

 

Basically my Boss wants me to create a page that will show a different plumbers details every time a user reaches the page.

 

He has 5 clients, and wants to rotate there contact details so they all given equal exposure on the site.

 

He's a challenging boss.... to say the least ( although i could say he's a **** too...lol)

Make a table with 1field count(INT(1)) - default value 0


$query = "SELECT `count` FROM table LIMIT 1";
$result = @mysql_query($query);
$row = mysql_fetch_row($result);

$count = $row[0];

$img = array();
$img[] = '<img src="img1.jpg" />';
$img[] = '<img src="img2.jpg" />';
$img[] = '<img src="img3.jpg" />';
$img[] = '<img src="img4.jpg" />';
$img[] = '<img src="img5.jpg" />';

echo $img[$count];

if ($count != 4){

   $count++;

}else{

   $count = 0;

}

mysql_query("UPDATE table SET `count` = '$count' LIMIT 1");

 

if you are worried about multiple users seeing the same information, then you will most likely need some kind of database.

 

since you can't use a session from one user on another user.

 

there is no real way to keep the 5 images rotating unless you used some sort of hit counter and then told the page to pull the image with the lowest hit count

You should probably use random images (which makes it more likely to see different plumbers) rather than what you were planning for the simple reason that it's more complicated than it has to be.  If you have it rotate every 5 for each visitor, what happens if your site is visited 46 times?  Then plumber #1 has had more exposure, for example.  Random images is probably the best way to go.  And I NEVER though I'd be in a discussion about plumber exposure on plumbing websites. ._.

You could do it in a txt file

make a file called count.txt with the value 0 written in it permissions 777


$img = array();
$img[] = '<img src="img1.jpg" />';
$img[] = '<img src="img2.jpg" />';
$img[] = '<img src="img3.jpg" />';
$img[] = '<img src="img4.jpg" />';
$img[] = '<img src="img5.jpg" />';

$count = (int)file_get_contents('count.txt');

echo $img[$count];

if ($count == count($img) - 1){
  $count = 0;
}else{
  $count++;
}

$file = 'count.txt';
$f = fopen($file, 'w');

fwrite($f, $count);
fclose($f);

 

I think thats how to do it but I only just started playing with these functions.

could a simple way to do it be to store the details in a database and store the id of the last queried image.

 

then every time a user loads the page get the next id and display that image that way it is rotated exactly equal?

 

(obviously it will require something to check if it is higher than the last id but thats not hard)

You could do it in a txt file

make a file called count.txt with the value 0 written in it permissions 777


$img = array();
$img[] = '<img src="img1.jpg" />';
$img[] = '<img src="img2.jpg" />';
$img[] = '<img src="img3.jpg" />';
$img[] = '<img src="img4.jpg" />';
$img[] = '<img src="img5.jpg" />';

$count = (int)file_get_contents('count.txt');

echo $img[$count];

if ($count == count($img) - 1){
  $count = 0;
}else{
  $count++;
}

$file = 'count.txt';
$f = fopen($file, 'w');

fwrite($f, $count);
fclose($f);

 

I think thats how to do it but I only just started playing with these functions.

 

 

Well that certainly seems to be doing the trick Andy .....phew. I think theres maybe a

medal waiting for you in the HR department lol :D

 

I've deleted all of my temporary files/cache then revisited the site and it's giving me a different plumber everytime now.

 

Could anyone confirm this?

might work test it...........

 

<?php session_start();

$img = array();
$img[] = '<img src="img1.jpg" />';
$img[] = '<img src="img2.jpg" />';
$img[] = '<img src="img3.jpg" />';
$img[] = '<img src="img4.jpg" />';
$img[] = '<img src="img5.jpg" />';

shuffle($img);

$_SESSION['go']=$img[0];

if($_SESSION['go']==$img[0]){

shuffle($img);

echo $_SESSION['go'];

}else{

echo $img[0];
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.