rodd Posted November 4, 2008 Share Posted November 4, 2008 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 https://forums.phpfreaks.com/topic/131400-display-a-different-image-whenever-a-new-user-visits/ Share on other sites More sharing options...
severndigital Posted November 4, 2008 Share Posted November 4, 2008 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 https://forums.phpfreaks.com/topic/131400-display-a-different-image-whenever-a-new-user-visits/#findComment-682401 Share on other sites More sharing options...
F1Fan Posted November 4, 2008 Share Posted November 4, 2008 You could use a DB table and store their IP address or something. Link to comment https://forums.phpfreaks.com/topic/131400-display-a-different-image-whenever-a-new-user-visits/#findComment-682404 Share on other sites More sharing options...
rodd Posted November 4, 2008 Author Share Posted November 4, 2008 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 https://forums.phpfreaks.com/topic/131400-display-a-different-image-whenever-a-new-user-visits/#findComment-682411 Share on other sites More sharing options...
F1Fan Posted November 4, 2008 Share Posted November 4, 2008 I vote IP. Link to comment https://forums.phpfreaks.com/topic/131400-display-a-different-image-whenever-a-new-user-visits/#findComment-682415 Share on other sites More sharing options...
DarkWater Posted November 4, 2008 Share Posted November 4, 2008 Can we see the code you used for random images? And IP isn't going to work too well. What if their IP changes? They could potentially always see the same image. Link to comment https://forums.phpfreaks.com/topic/131400-display-a-different-image-whenever-a-new-user-visits/#findComment-682416 Share on other sites More sharing options...
rodd Posted November 4, 2008 Author Share Posted November 4, 2008 thanks f1 fan, How would i begin to do that, im pretty new to all this. Is there a simple way? Link to comment https://forums.phpfreaks.com/topic/131400-display-a-different-image-whenever-a-new-user-visits/#findComment-682419 Share on other sites More sharing options...
severndigital Posted November 4, 2008 Share Posted November 4, 2008 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 https://forums.phpfreaks.com/topic/131400-display-a-different-image-whenever-a-new-user-visits/#findComment-682422 Share on other sites More sharing options...
rodd Posted November 4, 2008 Author Share Posted November 4, 2008 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 https://forums.phpfreaks.com/topic/131400-display-a-different-image-whenever-a-new-user-visits/#findComment-682423 Share on other sites More sharing options...
F1Fan Posted November 4, 2008 Share Posted November 4, 2008 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 https://forums.phpfreaks.com/topic/131400-display-a-different-image-whenever-a-new-user-visits/#findComment-682424 Share on other sites More sharing options...
rodd Posted November 4, 2008 Author Share Posted November 4, 2008 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 https://forums.phpfreaks.com/topic/131400-display-a-different-image-whenever-a-new-user-visits/#findComment-682428 Share on other sites More sharing options...
DarkWater Posted November 4, 2008 Share Posted November 4, 2008 <?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 https://forums.phpfreaks.com/topic/131400-display-a-different-image-whenever-a-new-user-visits/#findComment-682429 Share on other sites More sharing options...
rodd Posted November 4, 2008 Author Share Posted November 4, 2008 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 https://forums.phpfreaks.com/topic/131400-display-a-different-image-whenever-a-new-user-visits/#findComment-682432 Share on other sites More sharing options...
Andy-H Posted November 4, 2008 Share Posted November 4, 2008 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 https://forums.phpfreaks.com/topic/131400-display-a-different-image-whenever-a-new-user-visits/#findComment-682437 Share on other sites More sharing options...
severndigital Posted November 4, 2008 Share Posted November 4, 2008 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 https://forums.phpfreaks.com/topic/131400-display-a-different-image-whenever-a-new-user-visits/#findComment-682440 Share on other sites More sharing options...
DarkWater Posted November 4, 2008 Share Posted November 4, 2008 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 https://forums.phpfreaks.com/topic/131400-display-a-different-image-whenever-a-new-user-visits/#findComment-682444 Share on other sites More sharing options...
Andy-H Posted November 4, 2008 Share Posted November 4, 2008 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 https://forums.phpfreaks.com/topic/131400-display-a-different-image-whenever-a-new-user-visits/#findComment-682452 Share on other sites More sharing options...
blueman378 Posted November 4, 2008 Share Posted November 4, 2008 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 https://forums.phpfreaks.com/topic/131400-display-a-different-image-whenever-a-new-user-visits/#findComment-682469 Share on other sites More sharing options...
rodd Posted November 5, 2008 Author Share Posted November 5, 2008 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 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 https://forums.phpfreaks.com/topic/131400-display-a-different-image-whenever-a-new-user-visits/#findComment-682769 Share on other sites More sharing options...
redarrow Posted November 5, 2008 Share Posted November 5, 2008 sorry but from time to time there be shown the same image................... the only thing i can think off is holding the current img in a session and if it matches then relaod a diffent image... Link to comment https://forums.phpfreaks.com/topic/131400-display-a-different-image-whenever-a-new-user-visits/#findComment-682775 Share on other sites More sharing options...
redarrow Posted November 5, 2008 Share Posted November 5, 2008 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 https://forums.phpfreaks.com/topic/131400-display-a-different-image-whenever-a-new-user-visits/#findComment-682777 Share on other sites More sharing options...
Andy-H Posted November 9, 2008 Share Posted November 9, 2008 Works fine for me... Link to comment https://forums.phpfreaks.com/topic/131400-display-a-different-image-whenever-a-new-user-visits/#findComment-685760 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.