Which part of your code needs the random page? It looks like you'll have the entirety of the random code inside the if statement with $success, so:
if ($success)
{
//randomization and case handling
}
else
{
//print error page
}
As for actually doing the randoming and weighting, it's pretty easy. Let's say you have the following pages and want the following weights assigned:
Page 1 - 60%
Page 2 - 20%
Page 3 - 15%
Page 4 - 5%
What you'll need to do is find out the least common denominator over the total number of %'s. In this case, we have 100% total, so we put everything over 100. This gives us the following:
60/100 = 12/20
20/100 = 4/20
15/100 = 3/20
5/100 = 1/20
(Note: It isn't entirely necessary to reduce the fractions, but it keeps the numbers smaller and makes them a bit easier to manage, imo.) You'll notice that 12, 4, 3, and 1 add up to 20, so we have retained our percentages.
Next, you'll want to create a random number from 1-20, so we simply use:
$rand = mt_rand(1, 20);
Note: mt_rand is a newer random function that produces "better" random numbers.
Next, we'll want to create an if-else or switch structure to handle the random numbers. It'll look something like this:
if ($rand > 8) //20 - 12 = 8
{
//page1
}
elseif ($rand > 4) //20 - 12 - 4 = 4
{
//page2
}
elseif ($rand > 1) //20 - 12 - 4 - 3 = 1
{
//page3
}
else
{
//page4
}
Since the if-else will break as soon as one of the lines is true, you don't have to worry about setting a range for the second ones (i.e., you don't need to check to see whether $rand is between 4 and 8). The actual structure of the if-else or switch can be done probably fifty different ways, so if you don't like the method used above, feel free to do it differently. I'm sure there's a better way to do it, but that method was the easiest for me to not mess up. 😉