Tassos Marinos Developer of Joomla Extensions

Using the PHP Condition

Published in Extensions
Updated 19 Oct, 2021
Heads up! This article contains PHP code and is intended for developers. We offer this code as a courtesy, but don't provide support for code customizations or 3rd party development.

This guide walks you through several examples on how to use the PHP Condition available in most of the Tassos.gr extensions.

The PHP Condition gives you the opportunity to create advanced conditional scenarios for virtually any occassion you can think of. The only thing stopping you is having the PHP knowledge to code it and how far you are willing to go.

The main point is that you always need to return either a TRUE or a FALSE value. For TRUE the Condition is met and vice versa. Here's an example:

if ($variable == 'value') {
   return true;
} else {
   return false;
}

Or written in a more compact way:

return $some_variable == 'some value';

Ready-to-use PHP Variables & Objects

Below are a collection of ready-to-use PHP assignment scripts examples. You can also use these as examples and starting-point to create your own custom scripts. This means you don't have to create these variables yourself every time.

  • $app: The Joomla application framework
  • $doc: The Joomla document object
  • $db: The Joomla database object
  • $user: The user object containing the details of the guest or current logged in user
  • $Itemid: The ID of the current active menu item

Examples

Below are a (growing) collection of ready-to-use PHP assignment scripts. You can also use these as examples and starting-point to create your own custom scripts.

Browser version

Target users who are using a specific browser version

// Set the browser type and version
$browserType = "msie";
$browserMinimumVersion = "9";

// Do not edit below
jimport('joomla.environment.browser');
$browser = JBrowser::getInstance();

return ($browser->getBrowser() == $browserType && $browser->getMajor() <= $browserMinimumVersion);

Browser language

Target users who are using a specific language in their browser

// Set a comma separated list of accepted languages
$acceptLangs = "en, fr";

// Do not edit below
$browserLang = isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2) : false;

return in_array($browserLang, explode(',', $acceptLangs));

Joomla Articles

Target users who are browsing specific Joomla articles.

// Set a comma separated list of allowed articles
$articles = "50, 51, 52";

// Do not edit below
if ($app->input->getCmd('option') != "com_content")
{ 
	return false; 
}
if ($app->input->getCmd('view') == "article")
{ 
	return(in_array($app->input->getCmd('id'), explode(",", $articles))); 
}

Joomla Categories

Target users who are browsing Joomla articles from specific categories.

// Set a comma separated list of allowed categories
$categories = "50, 51, 52";

// Do not edit below
if ($app->input->getCmd('option') != "com_content")
{ 
	return false; 
}

if ($app->input->getCmd('view') == "category")
{ 
	return(in_array($app->input->getCmd('id'), explode(",", $categories))); 
}

IP address

Determine whether the user is behing a certain list of IP addresses.

// Set a comma separated list of IP addresses
$ips = '111.111.111.111, 222.222.222.222';

// Do not edit below
return (in_array($_SERVER['REMOTE_ADDR'], explode(',',$ips)));

Keyword present in page

Determine the given text is present in the content of the page.

// Set the string to be present in content
$search = 'my search string';

// Do not edit below
$buffer = $doc->getBuffer('component');
if (!is_array($buffer) && !(strpos($buffer, $search) === false)) {
    return true;
} else {
    return false;
}

User max pageviews

Determine whether the user reached the maximum number of pageviews on your site.

// Set the maximum number of pageviews
$max = 10;

// Do not edit below
$session = JFactory::getSession();
return $session->get('session.counter', 0) <= $max;

Referrer Sites

Determine whether the visitor came from one of the specified sites.

// Set a comma separated list of allowed domains
$domains = "facebook.com, twitter.com";
// Do not edit below if (!$referer = isset($_SERVER['HTTP_REFERER']) ? parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST) : false) { return; } $domain = str_ireplace("www.", "", $referer); return (in_array($domain, explode(',', $domains)));

Time Range

Determine whether current time is in certain time range.

// Enter your time range here
$startTime = 'today 09am';
$endTime   = 'today 11am';

// Do not edit below
$startDate = strtotime(JFactory::getDate($startTime));
$endDate   = strtotime(JFactory::getDate($endTime));
$now   = strtotime(JFactory::getDate()->setTimeZone(new DateTimeZone($app->getCfg('offset'))));

return ($now >= $startDate) && ($now <= $endDate);

Target Homepage

Determine whether the current page is the homepage.

return $app->getMenu()->getActive() == $app->getMenu()->getDefault();

Target Weekends (Saturday & Sunday)

Determine whether the current week day is Saturday or Sunday.

return in_array(JFactory::getDate()->format('N'), [6,7]);

User Cookies

Determine whether the visitor has certain cookies stored in their browser.

$cookies = ['some_cookie_a', 'some_cookie_b', 'some_cookie_c'];

// DO NOT EDIT BELOW
foreach ($cookies as $cookie)
{
    if (!$pass = (bool) $app->input->cookie->get($cookie))
    {
        return false;
    }
}

return true;

Exclude Bots, Crawlers and Spiders

Determine whether the visitor is a bot, crawler or spider by detecting common words in the user agent string. If you find a bot user agent that fails to be detected, add a common word/phrase in the $bot_identifiers variable.

// User lowercase string for comparison.
$user_agent = strtolower($app->input->server->get('HTTP_USER_AGENT'));

if (empty($user_agent))
{
    return false;
}

// A list of some common words used only for bots and crawlers.
$bot_identifiers = [
    'bot',
    'slurp',
    'crawler',
    'spider',
    'curl',
    'facebook',
    'fetch'
];

// See if one of the identifiers is in the UA string.
foreach ($bot_identifiers as $identifier)
{
    if (strpos($user_agent, $identifier) !== false)
    {
        return true;
    }
}

return false;