In the last article, we saw how to redirect users to a common page or to the page they came from after they log in. In this one, we’ll see how to redirect them based on the time or season.
Redirecting Users Based on the Time
Suppose you want to direct users to one landing page (12) during business hours and to another landing page (32) after hours and on weekends. This code assumes that the business is open from 9 am to 5 pm on weekdays.
Our Login tag would look like this:
[code language=”html”]
[[!Login? &loginResourceId=`[[!getResourceId]]`]]
[/code]
[code language=”php”]
/* getResourceId snippet */
if (! function_exists(‘isWeekend’)) {
function isWeekend($time) {
return (date(‘N’, $time) >= 6);
}
}
/* Get current time */
$time = time();
/* Is it a weekend? */
if (isWeekend($time)) {
$docId = 32;
} else {
/* Not a weekend, is it between 9 and 5? */
$open = strtotime(‘today + 9 hours’);
$close = strtotime(‘today + 17 hours’);
if ($time > $open && $time < $close) {
$docId = 12;
} else {
$docID = 32;
}
}
return $docId;
[/code]
Note that we’ve wrapped the isWeekend()
function in if (! function_exists(){}
. This might not be necessary, but if some other plugin happens to be using that function, or one with the same name, PHP would throw an error, so it’s good form to do this with functions that might be used elsewhere.
Redirecting Users Based on the Season
This is an easy one. We’ll use the code from the “Season” snippet that appeared in an earlier article. We’ll make that code into a function and call it to get the season name, then return the resource ID appropriate to the season.
Our Login tag would look the same as before, only the snippet content needs to change:
[code language=”html”]
[[!Login? &loginResourceId=`[[!getResourceId]]`]]
[/code]
[code language=”php”]
/* getResourceId snippet */
if (! function_exists(‘season’)) {
function season() {
$limits = array(
‘/12/21’ => ‘winter’,
‘/09/21’ => ‘fall’,
‘/06/21’ => ‘summer’,
‘/03/21’ => ‘spring’,
‘/01/01’ => ‘winter’
);
$adate = date("M d Y"); /* set $adate to today */
foreach ($limits AS $key => $value) {
$limit = date("Y") . $key;
if (strtotime($adate) >= strtotime($limit)) {
$season = $value;
}
}
return $season;
}
}
$season = season();
switch($season) {
case: ‘summer’:
$resourceId = 12;
break;
case: ‘fall’:
$resourceId = 22;
break;
case: ‘winter’:
$resourceId = 27;
break;
case: ‘spring’:
$resourceId = 32;
break;
}
return $resourceId;
[/code]
If your redirection scheme depends on who the user is, however, things get a little more complicated. In the next few articles, we’ll look at various methods for redirecting users in more sophisticated ways.
For more information on how to use MODX to create a web site, see my web site Bob’s Guides, or
better yet, buy my book: MODX: The Official Guide.
Looking for quality MODX Web Hosting? Look no further than Arvixe Web Hosting!