0

I have created a website ( link here ) where the breeders at the middle of the page are shown random whenever someone enters the page.

I was wondering if it would be possible that if someone goes from the homepage to another page, then presses back button, the breeder order to be the same as it was when he first entered the website.

Right now the order changes every time someone enters the homepage.

Radu
  • 524
  • 1
  • 6
  • 19
  • You could use his browser id (user-agent+ip) to create a unique id, use that as some kind of seed for your randomize algorith. How do you fetch the random entries? – Alexander Kludt Aug 29 '14 at 12:57
  • I'm randomizing them with php function shuffle(), then I show them. – Radu Aug 29 '14 at 13:01
  • I think you can use this answer http://stackoverflow.com/questions/6557805/randomize-a-php-array-with-a-seed and combine it with the user-agent+ip method to generate a seed that will always give you the same result per user. If you already have a session somehow you can also use the session id for the seed. – Alexander Kludt Aug 29 '14 at 13:03

2 Answers2

1

You can save the state from the order of your elements in a cookie and prevent the randomize function, if a cookie is set.

xyNNN
  • 492
  • 3
  • 21
  • Can I get an example? – Radu Aug 29 '14 at 12:49
  • How you randomize the elements? With which language do you reach your goal? – xyNNN Aug 29 '14 at 13:20
  • The website is integrated with wordpress and I'm using the shuffle() function from php to randomize them. – Radu Aug 29 '14 at 13:23
  • Tried this but now I have the cookie saved, and every time that same user enters the website a second time, he will see the first order, because now the cookie is saved. I will try Michel's solution, and thank you for your sugestion, it did put me on the right track. :) – Radu Aug 31 '14 at 17:34
1

Suppose you have the breeders per string, something like

$div[0]='<div>My breeder 1</div>';
$div[1]='<div>My breeder 2</div>';
$div[2]='<div>My breeder 3</div>';
$div[3]='<div>My breeder 4</div>';

and an array

$my_breeders=[0,1,2,3];

If you shuffle the array, you will get something like

$my_breeders=[1,3,2,0];

this is the order you display your divs in. Put this in a string and write it to a cookie.

$order=implode('-',$my_breeders);  //gives a string: 1-3-2-0
setcookie('breederorder',$order,time()+(30*60) )

the next time someone visits the page, check for the cookie

if(!empty($_COOKIE['breederorder'])){
   $my_breeders=explode('-',$_COOKIE['breederorder']);
  }

Now you have the same array as before.

Note, I set the cookie time at half an hour (30*60). If you set it too long, the next time a visitor comes to your page, het will still get the same order.

Michel
  • 4,076
  • 4
  • 34
  • 52
  • It seems like a nice solution, I'll try it first thing tomorrow morning and tell you how it goes. Thanks for being really inovative! – Radu Aug 31 '14 at 17:32