Display Page Chosen at Random

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jaysmizzle
Forum Newbie
Posts: 9
Joined: Fri May 01, 2009 2:55 am

Display Page Chosen at Random

Post by jaysmizzle »

Instead of having my display page designated as one single page (i.e. index.php), I'd like to have a display page that is chosen at random. For example, I have an index.php, about.php, and search.php page and every time someone visits my site they are taken to one of those pages at random. Does anyone know how to do this? Thanks for the help.
User avatar
it2051229
Forum Contributor
Posts: 312
Joined: Tue Dec 25, 2007 8:34 pm

Re: Display Page Chosen at Random

Post by it2051229 »

it is possible.. let's say they visit your index.php and let's say index.php will be the page to redirect the user to a random page so it would be like

Code: Select all

 
$randomValue = rand(0, 2); // generate a random number from 0 to 2
 
if($randomValue == 0)
{
      // redirect to search
      header("Location: search.php");
}
else if($randomValue == 1)
{
    // redirect to login
     header("Location: login.php");
}
else if($randomValue == 2)
{
    // redirect to whatever
      header("Location:whatever.php");
}
 
Last edited by Benjamin on Fri May 01, 2009 12:41 pm, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
danielwalters6
Forum Commoner
Posts: 31
Joined: Fri May 11, 2007 1:17 pm
Location: Cambridge, England, UK

Re: Display Page Chosen at Random

Post by danielwalters6 »

Thank you IT2051229! - I've managed to recylce this into something similar.
jaysmizzle
Forum Newbie
Posts: 9
Joined: Fri May 01, 2009 2:55 am

Re: Display Page Chosen at Random

Post by jaysmizzle »

Awesome, that worked beautifully. Thank you.
watson516
Forum Contributor
Posts: 198
Joined: Mon Mar 20, 2006 9:19 pm
Location: Hamilton, Ontario

Re: Display Page Chosen at Random

Post by watson516 »

A switch statement might be better to use and easier if you want to add additional pages later on.
jaysmizzle
Forum Newbie
Posts: 9
Joined: Fri May 01, 2009 2:55 am

Re: Display Page Chosen at Random

Post by jaysmizzle »

After applying this code to my index.php page, I noticed that when I click on a category it redirects me to one of the random pages from the code instead of the category's page. So while the code works in redirecting to one of the random pages it also has the adverse effect of redirecting me to one of those random pages when I click on a category as well. Anyone know how to get around this problem? Also, if Watson516 could expand on that switch statement, I'd be interested in that as well. Thanks.
mickd
Forum Contributor
Posts: 397
Joined: Tue Jun 21, 2005 9:05 am
Location: Australia

Re: Display Page Chosen at Random

Post by mickd »

Is it just the index page that redirects you to a random page? If this code isn't on the other pages, the other pages shouldn't be sending you to a random page too.

And this is a switch statement

Code: Select all

 
$value = rand(1, 2);
 
switch($value) {
   case 1:
      header('location: category.php');
      die();
      break;
   case 2:
      header('location: somewhere.php');
      die();
      break;
   default:
      // if none of the above cases are satisfied, it does this.
      header('location: somewhereelse.php');
      die();
      break;
}
 
Maybe post what you have done if you can't figure out why it's still giving you a random page, even when you click category :)
jaysmizzle
Forum Newbie
Posts: 9
Joined: Fri May 01, 2009 2:55 am

Re: Display Page Chosen at Random

Post by jaysmizzle »

It doesn't matter what page I'm on. Anytime I click on a category or on any page with index.php associated with it, I get redirected to one of the random pages. Basically, I just want visitors to my site directed to one of the categories at random whenever they visit my front page but when they actually click on a specific category I want them directed to that category, not a random one, obviously. I just tried the switch statement you posted and it does the same thing unfortunately. The code I'm using is pasted below. I have the same problem even when I substitute 'index.php?category=Ads-X' with something generic like 'register.php' so I know that's not the problem.

Code: Select all

 
$randomValue = rand(0, 3); // generate a random number from 0 to 3
 
if($randomValue == 0)
{
      // redirect to search
      header("Location: index.php?category=Ads-1");
}
else if($randomValue == 1)
{
    // redirect to login
     header("Location: index.php?category=Ads-2");
}
else if($randomValue == 2)
{
    // redirect to whatever
      header("Location: index.php?category=Ads-3");
}
 
else if($randomValue == 3)
{
    // redirect to whatever
      header("Location: index.php?category=Ads-4");
}
 
Last edited by Benjamin on Thu May 07, 2009 1:35 am, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Display Page Chosen at Random

Post by John Cartwright »

How do you determine the request if for the categories page? If you are using a $_GET variable, you can check for it's existance, and if not found, then proceed to the random pages.

I.e.,

Code: Select all

if (isset($_GET['page']) && $_GET['page'] == 'category') {
   header('Location: http://domain.com/category.php');
   exit();
} else {
   //random stuff
}
And please post your code using tags in the future.
jaysmizzle
Forum Newbie
Posts: 9
Joined: Fri May 01, 2009 2:55 am

Re: Display Page Chosen at Random

Post by jaysmizzle »

I'll just post the entire code for my index.php page. Let me know what you think.

Code: Select all

<?php
// The source code packaged with this file is Free Software, Copyright (C) 2005 by
// Ricardo Galli <gallir at uib dot es>.
// It's licensed under the AFFERO GENERAL PUBLIC LICENSE unless stated otherwise.
// You can get copies of the licenses here:
//      http://www.affero.org/oagpl.html
// AFFERO GENERAL PUBLIC LICENSE is also included in the file called "COPYING".
 
include_once('Smarty.class.php');
$main_smarty = new Smarty;
 
include('config.php');
include(mnminclude.'html1.php');
include(mnminclude.'link.php');
include(mnminclude.'tags.php');
include(mnminclude.'search.php');
include(mnminclude.'smartyvariables.php');
 
// module system hook
$vars = '';
check_actions('index_top', $vars);
 
 
 
$randomValue = rand(0, 3); // generate a random number from 0 to 3
 
if($randomValue == 0)
{
      // redirect to search
      header("Location: index.php?part=year&category=Ads-1.php");
}
else if($randomValue == 1)
{
    // redirect to login
     header("Location: index.php?part=year&category=Ads-2.php");
}
else if($randomValue == 2)
{
    // redirect to whatever
      header("Location: index.php?part=year&category=Ads-3.php");
}
 
else if($randomValue == 3)
{
    // redirect to whatever
      header("Location: index.php?part=year&category=Ads-4.php");
}
 
 
// find the name of the current category
if(isset($_REQUEST['category'])){
    $thecat = get_cached_category_data('category_safe_name', urlencode(sanitize($_REQUEST['category'], 1)));
    $thecat = $thecat->category_name;
}
 
// start a new search
$search=new Search();
 
// check for some get/post
if(isset($_REQUEST['from'])){$search->newerthan = sanitize($_REQUEST['from'], 3);}
if(isset($_REQUEST['search'])){$search->searchTerm = sanitize($_REQUEST['search'], 3);}
if(isset($_REQUEST['search'])){$search->filterToStatus = "all";}
if(!isset($_REQUEST['search'])){$search->orderBy = "link_published_date DESC";}
if(isset($_REQUEST['tag'])){$search->searchTerm = sanitize($_REQUEST['search'], 3); $search->isTag = true;}
if(isset($thecat)){$search->category = $thecat;}
 
// figure out what "page" of the results we're on
$search->offset = (get_current_page()-1)*$page_size;
 
// pagesize set in the admin panel
$search->pagesize = $page_size;
 
// since this is index, we only want to view "published" stories
$search->filterToStatus = "published";
 
// this is for the tabs on the top that filter
if(isset($_GET['part'])){$search->setmek = $db->escape($_GET['part']);}
$search->do_setmek();   
 
// do the search
$search->doSearch();
 
$linksum_count = $search->countsql;
$linksum_sql = $search->sql;
 
if(isset($_REQUEST['category'])) {
    $category_data = get_cached_category_data('category_safe_name', urlencode(sanitize($_REQUEST['category'], 1)));
    $main_smarty->assign('meta_description', $category_data->category_desc);
    $main_smarty->assign('meta_keywords', $category_data->category_keywords);
 
    // breadcrumbs and page title for the category we're looking at
    $main_smarty->assign('title', ''.$main_smarty->get_config_vars('PLIGG_Visual_Published_News').' - ' . $thecat . '');
    $navwhere['text1'] = $main_smarty->get_config_vars('PLIGG_Visual_Published_News');
    $navwhere['link1'] = getmyurl('root', '');
    $navwhere['text2'] = $thecat;
    $main_smarty->assign('navbar_where', $navwhere);
    $main_smarty->assign('pretitle', $thecat );
    $main_smarty->assign('posttitle', $main_smarty->get_config_vars('PLIGG_Visual_Published_News'));
    $main_smarty->assign('page_header', $thecat . $main_smarty->get_config_vars('PLIGG_Visual_Published_News'));
    // pagename 
    define('pagename', 'published'); 
    $main_smarty->assign('pagename', pagename);
} else {
    // breadcrumbs and page title
    $navwhere['show'] = 'yes';
    $navwhere['text1'] = $main_smarty->get_config_vars('PLIGG_Visual_Published_News');
    $navwhere['link1'] = getmyurl('root', '');
    $main_smarty->assign('navbar_where', $navwhere);
    $main_smarty->assign('posttitle', $main_smarty->get_config_vars('PLIGG_Visual_Home_Title'));
    $main_smarty->assign('page_header', $main_smarty->get_config_vars('PLIGG_Visual_Published_News'));
    // pagename 
    define('pagename', 'index'); 
    $main_smarty->assign('pagename', pagename);
}
 
//  make sure my_base_url is set
if($my_base_url == ''){echo '<center><span class=error>ERROR: my_base_url is not set. Please correct this using the <a href = "/admin/admin_config.php?page=Location%20Installed">admin panel</a>. Then refresh this page.</span></center>';}
 
// sidebar
$main_smarty = do_sidebar($main_smarty);
$sql = "SELECT user_login FROM " . table_users . " ORDER BY user_id DESC LIMIT 1";
$last_user = $db->get_var($sql);
$main_smarty->assign('last_user', $last_user);
 
// misc smarty
if(isset($from_text)){$main_smarty->assign('from_text', $from_text);}
if(isset($search->setmek)){$main_smarty->assign('setmeka', $search->setmek);}else{$main_smarty->assign('setmeka', '');}
 
$main_smarty->assign('URL_rss_page', getmyurl('rsspage', $category_data->category__auto_id, 'published'));
 
$fetch_link_summary = true;
include('./libs/link_summary.php'); // this is the code that show the links / stories
$main_smarty->assign('link_pagination', do_pages($rows, $page_size, "published", true));
 
// show the template
$main_smarty->assign('tpl_center', $the_template . '/index_center');
$main_smarty->display($the_template . '/pligg.tpl');
?>
 
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Display Page Chosen at Random

Post by John Cartwright »

I already explained what you need to do.
mickd
Forum Contributor
Posts: 397
Joined: Tue Jun 21, 2005 9:05 am
Location: Australia

Re: Display Page Chosen at Random

Post by mickd »

The reason why it doesn't work at the moment is that, the script works by executing down the page. The if statements with the header() functions in them are always executed before the next bit, which displays the rest of the page.

What you're doing is putting stuff into the URL to decide what page to show (and you're retrieving that information using $_REQUEST (which checks $_GET).

Have another look at what John said again. It will work =)

Basically, you'll be checking if $_GET (or $_REQUEST) has category and/or part set, and if it does, display the right page. If not, then do the random page displaying if statements.
Post Reply