Random Page

Doverembrigal

BANNED
Joined
Apr 19, 2014
Messages
21
Reaction score
1
Hello.

I have many pages, like 1.php, 2.php, 3.php ... . Sometimes I add new pages.

In the URL it is written as /?page=1, /?page=2, /?page=3 ... .

What I want is a link called "Random". If the visitor clicks on this link, he will be sent to a random page. So if I have 50 pages, he might be sent to 30.php or maybe to 17.php.

Could anyone help me out?
 
Try something like this:

PHP:
<?
$domain = "yourwebsite.com";  // change the domain to your site
$rand = mt_rand(1, 50); // random page between 1 to 50
header('Location: http://'. $domain . '/?page='. $rand);
exit;
?>
 
Hey my friend, this really works perfectly!!!! Thank you.

But I have one more problem.

If I add another page for example 51.php then I have to change it from mt_rand(1, 50) to mt_rand(1, 51) and if I add another page I have to change it from mt_rand(1, 51) to mt_rand(1, 52). So I always have to change it manually.

Is is possible that the code automatically detects what's the highest number so it changes the mt_rand automatically?

This would help me a lot.

Regards, Dove.
 
It's possible, but it depends on the way you have everything setup.

If you have all of those .php files in a directory with nothing else besides those files, then you could come up with php code to count total files in that directory and use that number as the max integer.

(feel free to click the thanks button if i helped) :)
 
This should work...

PHP:
<?
$files = array();
$dir = opendir('.'); 
while(false != ($file = readdir($dir))) {
        if(($file != ".") and ($file != "..") and !is_dir($file)) {
                $files[] = $file; 
        }   
}
if ( $files !== false ) {
    $filecount = count( $files );
    $filecount = $filecount - 1;
    echo $filecount;
} else {
    echo 0;
}


$domain = "yourwebsite.com";  // change the domain to your site
$rand = mt_rand(1, $filecount);
header('Location: http://'. $domain . '/?page='. $rand);
exit;
?>
 
How can I change the directory? The files are not in public_html, they are in a sub-folder. Like public_html/sub-folder.
 
try changing this $dir = opendir('.'); to $dir = opendir('/public_html/sub-folder/');
 
Okay. Now it opens ?page=1 everytime I click the Link. Not random anymore, everytime ?page=1.
 
hmmmm...

Try this one: (change the second line to the correct name)

PHP:
<?
$files = array();
$dir = opendir('./sub-folder'); 
while(false != ($file = readdir($dir))) {
        if(($file != ".") and ($file != "..") and !is_dir($file)) {
                $files[] = $file; 
        }   
}
if ( $files !== false ) {
    $filecount = count( $files );
    $filecount = $filecount;
    //echo $filecount; exit;
} else {
    echo 0;
}


$domain = "yourwebsite.com";  // change the domain to your site
$rand = mt_rand(1, $filecount);
header('Location: http://'. $domain . '/?page='. $rand);
exit;
?>
 
I guess it works now. I had to add a "-1" after $filecount. I guess you just forgot it in your last version? Because in the previous one you had it there^^

Thank you very much!
 
I just about to post about creating a random page display on my website and I've seen this post. i had to thank you also for this. :)
.
Helped me too alot. thanks. :)
 
Back
Top