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

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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??

 

Link to comment
Share on other sites

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];
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

The main thing is that every user sees a different image.

 

It doesnt matter if user A refreshes and sees the same image again, providing user B doesnt see the same image as A did the FIRST time entering the site.

 

Thanks again.

Link to comment
Share on other sites

<?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?

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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");

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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. ._.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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];
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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