1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Random Page

Discussion in 'PHP & Perl' started by Dove1992, Aug 6, 2014.

  1. Dove1992

    Dove1992 Newbie

    Joined:
    Apr 19, 2014
    Messages:
    21
    Likes Received:
    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?
     
  2. Automated

    Automated Regular Member

    Joined:
    Jun 7, 2012
    Messages:
    289
    Likes Received:
    123
    Location:
    Online
    Try something like this:

    PHP:
    <?
    $domain "yourwebsite.com";  // change the domain to your site
    $rand mt_rand(150); // random page between 1 to 50
    header('Location: http://'$domain '/?page='$rand);
    exit;
    ?>
     
    • Thanks Thanks x 1
  3. Dove1992

    Dove1992 Newbie

    Joined:
    Apr 19, 2014
    Messages:
    21
    Likes Received:
    1
    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.
     
  4. Automated

    Automated Regular Member

    Joined:
    Jun 7, 2012
    Messages:
    289
    Likes Received:
    123
    Location:
    Online
    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) :)
     
    • Thanks Thanks x 1
  5. Automated

    Automated Regular Member

    Joined:
    Jun 7, 2012
    Messages:
    289
    Likes Received:
    123
    Location:
    Online
    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;
    ?>
     
  6. Dove1992

    Dove1992 Newbie

    Joined:
    Apr 19, 2014
    Messages:
    21
    Likes Received:
    1
    How can I change the directory? The files are not in public_html, they are in a sub-folder. Like public_html/sub-folder.
     
  7. Automated

    Automated Regular Member

    Joined:
    Jun 7, 2012
    Messages:
    289
    Likes Received:
    123
    Location:
    Online
    try changing this $dir = opendir('.'); to $dir = opendir('/public_html/sub-folder/');
     
  8. Dove1992

    Dove1992 Newbie

    Joined:
    Apr 19, 2014
    Messages:
    21
    Likes Received:
    1
    Okay. Now it opens ?page=1 everytime I click the Link. Not random anymore, everytime ?page=1.
     
  9. Automated

    Automated Regular Member

    Joined:
    Jun 7, 2012
    Messages:
    289
    Likes Received:
    123
    Location:
    Online
    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;
    ?>
     
    • Thanks Thanks x 1
  10. Dove1992

    Dove1992 Newbie

    Joined:
    Apr 19, 2014
    Messages:
    21
    Likes Received:
    1
    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!
     
  11. member8200

    member8200 Regular Member

    Joined:
    Aug 9, 2014
    Messages:
    469
    Likes Received:
    33
    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. :)
     
  12. bimbimpr

    bimbimpr Junior Member Premium Member

    Joined:
    May 22, 2014
    Messages:
    100
    Likes Received:
    19
    Try just include cfg.php with declarating global max