MyBB Community Forums

Full Version: Little Bit of PHP Help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hey guys, I need some help with a small PHP snippet.

I want to split traffic on my website by redirecting a form to two separate files (file1.php and file2.php).

A possible way of doing this is to randomize the process by selected 1 or 2 randomly and then redirecting the person using that selected number.

However, is there a way of doing it not randomly? I want it so that the first person goes to file1.php, then the second person goes to file2.php, then the third person goes back to file1.php, etc. I know how to count numbers with PHP, but I don't know the best way to set it back to 1 after 2.

Thanks a bunch Smile
You'd need to log which form was last shown either by writing to a file or to the database.
If multiple users go that page at the same time you'll probably get a database crash or a file corruption.

I'd go with the random way
I would, but it just seems that random is never correct in terms of theoretical probability. I'm doing this because I'm testing out two different ads, and I really want the traffic to be as even as possible for the sake of testing.

Is it possible to just continue adding plus 1 and then doing it based on odd/even numbers? (By the way, I have no problem writing to a .txt file for the sake of counting)
(2010-08-14, 11:28 PM)Zash Wrote: [ -> ]I would, but it just seems that random is never correct in terms of theoretical probability. I'm doing this because I'm testing out two different ads, and I really want the traffic to be as even as possible for the sake of testing.

Is it possible to just continue adding plus 1 and then doing it based on odd/even numbers? (By the way, I have no problem writing to a .txt file for the sake of counting)

I don't recommend it. The file will probably end up as corrupted or to prevent file corruption the system will not allow another instance of the file to be opened.

That's just not practical
Then maybe the best thing for this to do is redirect based on the time of day? (Check if the minutes are odd or even, and redirect on that).
(2010-08-14, 11:33 PM)Zash Wrote: [ -> ]Then maybe the best thing for this to do is redirect based on the time of day? (Check if the minutes are odd or even, and redirect on that).

Yes that would work. I'd go with seconds though even though it will still be random since you don't know when the users will visit your website.
I would stick with the random. If you want to make it "more random" you need to follow the practices I follow in my MUD code.

(The examples below are in C, and use a custom rand_number() function, but should be obvious)
Instead of rand_number(1,4) == 1 for a 1 in 4 chance, I do rand_number(1, 100) < 26. For a 50% change, you would do rand_number(1, 100) < 51. This brings you closer to a proper "chace". Its still off, as is all computerized random number generation, but its "more" random.

For your example I would do
if(rand(1,100) < 51)
  file1
else
  file2
The greater the numbers we're dealing with, the more likely it will be random :p (And at the same time, that's not true lol)

Thanks, I'll probably just stick with that then if there are no other options.
(2010-08-14, 11:36 PM)Zash Wrote: [ -> ]The greater the numbers we're dealing with, the more likely it will be random :p (And at the same time, that's not true lol)

Thanks, I'll probably just stick with that then if there are no other options.

Both that and the "by second" idea are workable. Really depends on personal preference.
Alright, one more question. How can I take the value returned for a user and put it in a cookie? So that way, the user will remain on that same value for 24 hours.
Pages: 1 2